Canvas: trying to use a recycled bitmap android.graphics.Bitmap in Android
Solution 1:
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;
}
Solution 2:
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);
Solution 3:
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.
Solution 4:
Android doesn't allows us reuse recycled Bitmap
.just comment the bitmap.recycle()
to resolve this error.
For more details click here
Solution 5:
In my case, i have inflate a layout containing imageview with src image where i have faced same error. That case, problem could be resolved by adding source image programmatically like:
((ImageView)view.findViewById(R.id.imageview)).setImageBitmap(BitmapFactory.decodeResource(getContext().getResources(),
R.drawable.testimage));