Monday, October 25, 2010

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

Recently our two applications on Android Market kept reporting error “java.lang.RuntimeException: Canvas: trying to use a recycled bitmap android.graphics.Bitmap…”. If you look at the stack trace, there is no trace of our code. Below is one of the stack trace:

java.lang.RuntimeException: Canvas: trying to use a recycled bitmap android.graphics.Bitmap@4478a900
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:842)
at android.view.View.draw(View.java:6740)
at android.view.ViewRoot.draw(ViewRoot.java:1407)
at android.view.ViewRoot.performTraversals(ViewRoot.java:1163)
at android.view.ViewRoot.handleMessage(ViewRoot.java:1727)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4627)
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:868)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
at dalvik.system.NativeStart.main(Native Method)

 

After searching around the Internet, I found some clues. It has something to do with the call of bitmap.recycle(). Some people suggest to remove all the calls to bitmap.recycle() and let the garbage collector to do the job. So I tried this solution and updated the Packing List application. I will give it a day or two to see if it settles the issue.

2 comments:

  1. first you should not remove bitmap.recycle() function, removing it may cause Out Of Memory Error. "trying to use a recycled bitmap" exception occurs when you try to recycle a already recycled bitmap.

    Better you can use the below solution to fix the issue.

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

    note: profileWallpaper is your bitmap imageView

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

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

    log("bitmap is already recycled");
    }

    bitmapDrawable = null;

    ReplyDelete
  2. but why does this problem occurs

    ReplyDelete