artem
artem

Reputation: 16777

How to make Android not to recycle my Bitmap until I don't need it?

I'm getting drawing cache of the view, that is set as contentView to the Activity. Then I set new content view to the activity and pass that drawing cache to it. But Android recycles my bitmaps and I'm getting this exception:

06-13 01:58:04.132: E/AndroidRuntime(15106): java.lang.RuntimeException: Canvas: trying to use a recycled bitmap android.graphics.Bitmap@40e72dd8

Any way to fix it? I had an idea to extend Bitmap class, but it's final. Why GC is recycling it?

Upvotes: 1

Views: 748

Answers (2)

artem
artem

Reputation: 16777

Problem solved, I just cloned the bitmap to new. I think, exception occurred because the cached view was removed with it's drawing cache.

Upvotes: 1

dmon
dmon

Reputation: 30168

A bitmap being recycled does not mean it was Garbage Collected, it means Bitmap.recycle() was called on it, on purpose. I'm not familiar with the specific code, but I'm pretty sure that when you call setContentView() with something new, it will tear down the previous views (since it's assumed that you don't want them anymore). It seems to be hitting an ImageView and that calls recycle() on its Bitmap to be a good citizen, which then gives you your error. My guess is that what you're trying to do is not supported.

Upvotes: 2

Related Questions