Android How to - Recycle a Bitmap








Question

We would like to know how to recycle a Bitmap.

Answer

/*from  w  w  w .j  a  v a  2 s . co  m*/
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.widget.ImageView;

public class Main {
  public static void recycleBitmap(ImageView iv) {
        Drawable d = iv.getDrawable();
        if (d instanceof BitmapDrawable) {
            Bitmap b = ((BitmapDrawable)d).getBitmap();
            b.recycle();
        }
         
        d.setCallback(null);
    }
  
}