Question

Let's say I have code sort of like the one below:

protected void onCreate(Bundle bundle){

    this.imageView = (ImageView) contentView.findViewById(R.id.imageView);

    this.setFirstBitmap();
    this.setSecondBitmap();
}

private setFirstBitmap(){
    Bitmap bitmap1 = BitmapFactory.decodeFile(bitmapFile1);
    imageView.setImageBitmap(bitmap1);
}

private setSecondBitmap(){
    Bitmap bitmap2 = BitmapFactory.decodeFile(bitmapFile2);
    imageView.setImageBitmap(bitmap2);
}

In this case, will the imageView recycle bitmap1 or do I have to do it before I set bitmap2?

Was it helpful?

Solution

ImageView doesnt release the bitmaps automatically

It happens as explained by @Vipul

Bitmaps reference must be released by calling bitmap.recycle()

When you want to assign another bitmap to the ImageView recycle the previous by calling

((BitmapDrawable)imageView.getDrawable()).getBitmap().recycle();

Take a look at this

OTHER TIPS

When you try to decode and set second bitmap Java will tell to GC that first bitmap need to be recycled since developer is no longer using it.GC will do it later.

But if you making extensive use of Bitmaps (speed of allocation may be greater than speed at which bitmap getting recycled) then you might want to recycle unused bitmaps ASAP.You should call recycle() when you are done using the bitmap. ( Always remember don't try to recycle bitmap when it is being shown on the screen.)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top