Android trying to use a recycled bitmap, not in my code-ThrowExceptions

Exception or error:

I get this stacktrace from the Market developer console every once in a while; I can’t find any way to repro the error. It’s happening while displaying a splashscreen ImageView as the app is first loading, but the stacktrace doesn’t have any of my code. I don’t even think my activity has even reached onCreate, though it’s hard to tell without a log.

Indeed, I never actually use a Bitmap anywhere in my code; my only reference to the image is in my layout.xml.

<ImageView android:id="@+id/splashScreen"
  android:layout_height="fill_parent"
  android:layout_width="fill_parent"
  android:src="@drawable/splashscreen"
  android:scaleType="fitXY"
/>

The only thing I do with this ImageView is set its visibility to GONE when I’m finished starting.

Is there anything I can do about this?

java.lang.RuntimeException: Canvas: trying to use a recycled bitmap android.graphics.Bitmap@4721ec18
at android.graphics.Canvas.throwIfRecycled(Canvas.java:955)
at android.graphics.Canvas.drawBitmap(Canvas.java:1044)
at android.graphics.drawable.BitmapDrawable.draw(BitmapDrawable.java:323)
at android.widget.ImageView.onDraw(ImageView.java:923)
at android.view.View.draw(View.java:6761)
at android.view.ViewGroup.drawChild(ViewGroup.java:1663)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1390)
at android.view.View.buildDrawingCache(View.java:6517)
at android.view.View.getDrawingCache(View.java:6305)
at android.view.ViewGroup.drawChild(ViewGroup.java:1588)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1390)
at android.view.ViewGroup.drawChild(ViewGroup.java:1661)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1409)
at android.view.View.draw(View.java:6764)
at android.widget.FrameLayout.draw(FrameLayout.java:352)
at android.view.ViewGroup.drawChild(ViewGroup.java:1663)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1390)
at android.view.ViewGroup.drawChild(ViewGroup.java:1661)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1390)
at android.view.ViewGroup.drawChild(ViewGroup.java:1661)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1390)
at android.view.View.draw(View.java:6764)
at android.widget.FrameLayout.draw(FrameLayout.java:352)
at android.view.ViewGroup.drawChild(ViewGroup.java:1663)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1390)
at android.view.ViewGroup.drawChild(ViewGroup.java:1661)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1390)
at android.view.View.draw(View.java:6764)
at android.widget.FrameLayout.draw(FrameLayout.java:352)
at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:1887)
at android.view.ViewRoot.draw(ViewRoot.java:1432)
at android.view.ViewRoot.performTraversals(ViewRoot.java:1167)
at android.view.ViewRoot.handleMessage(ViewRoot.java:1764)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:143)
at android.app.ActivityThread.main(ActivityThread.java:5068)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
at dalvik.system.NativeStart.main(Native Method)
How to solve:

This exception occurs when you try to recycle or use a recycled bitmap. You should not remove bitmap.recycle() function from your existing code, it may cause Out Of Memory Error. You can try this below code to fix the issue.

BitmapDrawable bitmapDrawable = ((BitmapDrawable) profileWallpaper.getDrawable());

                if (null != bitmapDrawable && !bitmapDrawable.getBitmap().isRecycled()) {

                    bitmapDrawable.getBitmap().recycle();
                } else {

                    log("bitmap is already recycled");
                }

                bitmapDrawable = null;

Note: profileWallpaper is your imageView object

###

If you recycle your bitmap somewhere in your code, you should be sure that, when the app restart, your bitmap would be recreated.
When you pass through the onCreate, no issues, but when the app is in pause and restarted it will not pass through the onCreate again, just through the onResume.
I think you could recreate your bitmaps in the onResume method, but use a flag to create the bitmap in the onCreate or in the onResume, not both (onCreate and onResume are called together when the app starts and you could waste CPU cycles).

###

You should not use same image resources for different Activities while one of them is recycling its bitmap.

If you have this situation, changed the image resource of one of those Activities.

###

Have you tried to lower the ress of the image? It happened to me lots of times. If the problem continues after rezise the image, it means that the problem is in somewere else… I would look in the on create methode from your main activity or in the splashscreen code.

sorry my crappy english 🙂

C ya!

###

Just solved the issue with similar crash log. In my case it was just second ProgressBar inside the fragment. I had two lists inside it and ProgressBars for each of them. Removing the second one solved the problem. I can only guess that they were using the same bitmap resource for background or animation…

I’ve spent a lot of time to understand its reason, and it was just a standart ProgressBar! :/
Hope this help someone to save the time and peace of mind.

###

I was having the same Exception that is not coming from my application and found out that it is not related to AdMob.

The issue arises when you recycle a BitmapDrawable resource that was set through XML to your ImageView.

While resuming, android doesn’t decode the image again whether it is recycled or not, therefore, you are getting Canvas: trying to use a recycled bitmap.

The way to get solve this issue is to reload the corresponding BitmapDrawable at onPause of your Activity so that android would not complain about the bitmap being recyced.

Here is how to do it:

Bitmap selectImage = BitmapFactory.decodeResource(getResources(), R.drawable.your_bitmap_drawable);
yourImageView.setImageBitmap(selectImage);

Edit:
I realized simply setting Drawable of yourImageView to null also prevents this error. Just don’t leave a Drawable that has a recycled Bitmap. Example code:

yourImageView.setImageDrawable(null);

Leave a Reply

Your email address will not be published. Required fields are marked *