Android_Code_Chef
Android_Code_Chef

Reputation: 915

trying to use a recycled bitmap android.graphics.Bitmap

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)

Upvotes: 3

Views: 18793

Answers (3)

Yonko Kilasi
Yonko Kilasi

Reputation: 341

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; 
}

Upvotes: 0

Zala Janaksinh
Zala Janaksinh

Reputation: 2927

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.

Upvotes: 15

Alécio Carvalho
Alécio Carvalho

Reputation: 13647

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.

Upvotes: 0

Related Questions