Canvas: trying to use a recycled bitmap android

You need to copy the source image first:

Bitmap bitmap = webview.getDrawingCache();
if (bitmap != null) {
        Snapshot.CroppedBitmap.setImageBitmap(bitmap.copy(bitmap.getConfig(), false));
}
  • Using createBitmap will not necessarily create a copy of the source bitmap, resulting the same error.

Copy the bitmap before you pass it to the static variable.

Snapshot.CroppedBitmap = imageView.getDrawingCache(true);

to

Snapshot.CroppedBitmap = Bitmap.createBitmap(imageView.getDrawingCache(true));

It is very likely that the imageView is recycling its drawing cache when no longer needed as good practice. By copying it out, you keep a reference to the bitmap the ImageView trashed. Just make a copy of it that you can manage yourself!

Tags:

Android

Bitmap