user155
user155

Reputation: 805

Checking if bitmap is recycled doesn't help to solve "RuntimeException: Canvas: trying to use a recycled bitmap"

I send bitmaps to my activity from Service (using LocalBroadcastManager and intent bitmap extra)

I set them to ImageView in activity like this:

if (!bitmap.isRecycled) {
    imageView.setImageBitmap(bitmap)
}

When I don't need the service anymore I close it and release bitmap object kept in that service

But when I close the service I usually get the following error:

java.lang.RuntimeException: Canvas: trying to use a recycled bitmap android.graphics.Bitmap@fc4b896

Sobitmap.isRecycled condition doesn't help here

Is there any safe method to set Bitmap to ImageView?

Upvotes: 0

Views: 3563

Answers (2)

Bach Vu
Bach Vu

Reputation: 2348

When you are using imageView.setImageBitmap(bitmap), you should not recycle the bitmap after setImageBitmap because your imageView is still referencing and using that bitmap. You can recycle it when activity/fragment/view is destroyed.

Upvotes: 1

Ankit Tale
Ankit Tale

Reputation: 2004

You exception is telling you that bitmap is already recycled so no need to app check

java.lang.RuntimeException: Canvas: trying to use a recycled bitmap android.graphics.Bitmap@fc4b896

instead just check for null value

Also check documentation why you got an error

https://developer.android.com/reference/android/graphics/Bitmap#isRecycled()

Upvotes: 1

Related Questions