3

I am displaying one .gif in my activity A. When user presses one button user moves to activity b and same .gif is displaying there but I am getting "trying to use a recycled bitmap android.graphics.Bitmap" this error on my activity b.

I am displaying .gif from sd card and on onStop() I am setting imageview of activity a to null as well in activity A but I am not able to solve this issue.

Please help in this case.

//Log Cat

12-31 10:58:49.819: E/AndroidRuntime(20903): java.lang.RuntimeException: Canvas: trying to use a recycled bitmap android.graphics.Bitmap@405131c8
12-31 10:58:49.819: E/AndroidRuntime(20903):     at android.graphics.Canvas.throwIfRecycled(Canvas.java:955)
12-31 10:58:49.819: E/AndroidRuntime(20903):     at android.graphics.Canvas.drawBitmap(Canvas.java:1044)
12-31 10:58:49.819: E/AndroidRuntime(20903):     at android.graphics.drawable.BitmapDrawable.draw(BitmapDrawable.java:325)
12-31 10:58:49.819: E/AndroidRuntime(20903):     at android.widget.ImageView.onDraw(ImageView.java:854)
12-31 10:58:49.819: E/AndroidRuntime(20903):     at android.view.View.draw(View.java:6880)
12-31 10:58:49.819: E/AndroidRuntime(20903):     at android.view.ViewGroup.drawChild(ViewGroup.java:1646)
12-31 10:58:49.819: E/AndroidRuntime(20903):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
12-31 10:58:49.819: E/AndroidRuntime(20903):     at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
12-31 10:58:49.819: E/AndroidRuntime(20903):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
12-31 10:58:49.819: E/AndroidRuntime(20903):     at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
12-31 10:58:49.819: E/AndroidRuntime(20903):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
12-31 10:58:49.819: E/AndroidRuntime(20903):     at android.view.View.draw(View.java:6883)
12-31 10:58:49.819: E/AndroidRuntime(20903):     at android.view.ViewGroup.drawChild(ViewGroup.java:1646)
12-31 10:58:49.819: E/AndroidRuntime(20903):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
12-31 10:58:49.819: E/AndroidRuntime(20903):     at android.view.View.draw(View.java:6883)
12-31 10:58:49.819: E/AndroidRuntime(20903):     at android.widget.FrameLayout.draw(FrameLayout.java:357)
12-31 10:58:49.819: E/AndroidRuntime(20903):     at android.view.ViewGroup.drawChild(ViewGroup.java:1646)
12-31 10:58:49.819: E/AndroidRuntime(20903):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
12-31 10:58:49.819: E/AndroidRuntime(20903):     at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
12-31 10:58:49.819: E/AndroidRuntime(20903):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
12-31 10:58:49.819: E/AndroidRuntime(20903):     at android.view.View.draw(View.java:6883)
12-31 10:58:49.819: E/AndroidRuntime(20903):     at android.widget.FrameLayout.draw(FrameLayout.java:357)
12-31 10:58:49.819: E/AndroidRuntime(20903):     at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:1871)
12-31 10:58:49.819: E/AndroidRuntime(20903):     at android.view.ViewRoot.draw(ViewRoot.java:1542)
12-31 10:58:49.819: E/AndroidRuntime(20903):     at android.view.ViewRoot.performTraversals(ViewRoot.java:1269)
12-31 10:58:49.819: E/AndroidRuntime(20903):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1883)
12-31 10:58:49.819: E/AndroidRuntime(20903):     at android.os.Handler.dispatchMessage(Handler.java:99)
12-31 10:58:49.819: E/AndroidRuntime(20903):     at android.os.Looper.loop(Looper.java:130)
12-31 10:58:49.819: E/AndroidRuntime(20903):     at android.app.ActivityThread.main(ActivityThread.java:3737)
12-31 10:58:49.819: E/AndroidRuntime(20903):     at java.lang.reflect.Method.invokeNative(Native Method)
12-31 10:58:49.819: E/AndroidRuntime(20903):     at java.lang.reflect.Method.invoke(Method.java:507)
12-31 10:58:49.819: E/AndroidRuntime(20903):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:894)
12-31 10:58:49.819: E/AndroidRuntime(20903):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:652)
12-31 10:58:49.819: E/AndroidRuntime(20903):     at dalvik.system.NativeStart.main(Native Method)
stealthjong
  • 10,858
  • 13
  • 45
  • 84
Android_Code_Chef
  • 905
  • 4
  • 13
  • 20
  • 2
    Please show us some code and your logcat. – Lawrence Choy Jan 03 '13 at 09:26
  • It's quite difficult to give you an answer without looking at the code. however what happen is this. when you set ImageView to null, it may recycle resources and recycle the bitmap. but if you decode the bitmap in activity B, you shouldn't get this error. if your image is large one and if you think you will get OOM error, consider to use bitmap options and decode image to the size of the ImageView – Asanka Senavirathna Jan 03 '13 at 10:05
  • I am assuming that you are using facebook slide out library...It is because of the recycling of bitmap in the libray you've used for the slide out.. I have got the same exception and I have managed to fix it.. – Pragnani Feb 26 '13 at 13:26

3 Answers3

15

You can have check before recycling the bitmap, as like :

if (img != null && !img.isRecycled()) 
{
 img.recycle(); 
 img = null;
 System.gc(); 
}

Here img is bitmap. Try this type error is solved.

Zala Janaksinh
  • 2,929
  • 5
  • 32
  • 58
  • 4
    Sometimes I'd like to have the power to "delete" some user, just because they don't accept an answer! – JJ86 Mar 13 '14 at 17:04
  • @RvPanchal what is the problem.. In this code? can you explain here? – Zala Janaksinh Apr 20 '15 at 04:24
  • @RvPanchal can you post your code here.. Because I already check about that .. if Bitmap is recycled then recycled them.. just check your code. – Zala Janaksinh Apr 20 '15 at 05:46
  • This is My code if(bi != null && !bi.isRecycled()){ bi.recycle(); bi = null; } bi = mPdfPage.getImage((Constants.SCREEN_WIDTH * 2), (Constants.SCREEN_HEIGHT * 2), clip, true, true); // SaveBitmap(bi, str); return bi; – Ravi Makvana Apr 20 '15 at 05:52
  • just create your bitmap is mutable.. your issue is solved.Because you use immutable bitmap another time to modified. like String.. just read some things about mutable vs immutable bitmap. your issue is solved. it's not the problem in my code. – Zala Janaksinh Apr 20 '15 at 06:04
  • I think you don't need to recycle bitmap manually. Android v3.0 onwards does it internally. another thing is that you could use `BitmapFactory.Options.inBitmap` and `inSampleSize` for decoding larger bitmaps. – Amol Desai Nov 09 '15 at 09:20
0

Try to find references of usage of the method recycle() in your code or in any library that you use. It indicates that someone is recycling a bitmap that is still being used which is not good. You can only safely call the recycle() method when you are absolutely sure that the given bitmap won't be necessary anymore.

It is usually a common problem when you are dealing with bitmap caches, not sure if that is your case.

Alécio Carvalho
  • 13,481
  • 5
  • 68
  • 74
0

Solution already provided here : Canvas: trying to use a recycled bitmap android.graphics.Bitmap in android

This one should work . Otherwise checkout the link for more solutions

if (mBitmap != null && !mBitmap.isRecycled()) {
    mBitmap.recycle();
    mBitmap = null; 
}
Yonko Kilasi
  • 337
  • 3
  • 9