Canvas: trying to use a recycled bitmap android.graphics.Bitmap in Android

Android doesn't allows us reuse recycled Bitmap .just comment the bitmap.recycle() to resolve this error. For more details click here


For those that did not find a solution so far. I had the same problem. I tried to recycle a bitmap in onPause like this:

final Drawable drawable = mImageView.getDrawable();

if (drawable instanceof BitmapDrawable) {
    BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
    Bitmap bitmap = bitmapDrawable.getBitmap();
    bitmap.recycle();
}

if (preView != null && !preView.isRecycled()) {
    preView.recycle();
    preView = null;
}

After returning back i got the exception: Canvas: trying to use a recycled bitmap

Solution for me: I had to add the following

mImageView.setImageBitmap(null);

In my case error was caused because I changed visibility from gone to visible (or vice versa) of an element of the layout. And as consequence the space for the imageview and the bitmap created changed, so recycling caused app to crash. Avoid this and your problem will be fix.


Try to add this before calling recycle() methods to make sure bitmap isn't already recycled:

if (mBitmap != null && !mBitmap.isRecycled()) {
    mBitmap.recycle();
    mBitmap = null; 
}