Does ImageView.setImageBitmap() recycle the previously set bitmap?

W.K.S picture W.K.S · Mar 17, 2014 · Viewed 20.1k times · Source

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?

Answer

Blackbeard picture Blackbeard · Mar 17, 2014

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