Question

Disclaimer: I read about 20 existing questions. Unfortunately none of them solved my problem.

I have an activity. This activity gets a Bitmap from a global/static class C and sets the bitmap to an ImageView. When the activity is finished and I try to reopen the activity, I get the java.lang.RuntimeException: Canvas: trying to use a recycled bitmap exception. I don't call Bitmap.recycle() at any point. Also, I do not delete the bitmap reference in class C. I tried the following but they didn't work:

  • Call ImageView.setDrawingCacheEnabled(false)
  • Before setting the bitmap for ImageView, make a deep copy (via Bitmap.copy() method)

Also, I put some log statements throughout the activity lifecycle. Inside onDestroy(), before calling super.onDestroy() I check and make sure that the bitmap is not recycled (through Bitmap.isRecycled())

How can I fix this issue?

Was it helpful?

Solution

The culprit turned out to be RemoteControlClient.MetadataEditor.apply(), it does call recycle() for bitmaps passed to it.

OTHER TIPS

I had similar issue with ImageViews when reopening the activity (e.g. after screen is waked). The problem lies not in Bitmap itself, but in ImageView. My solution was to set ImageView's setImageBitmap to null on activity's onPause() and call setImageBitmap(theBitmap) on activity's onResume().

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top