首页 > 代码库 > android 如何分析java.lang.IllegalArgumentException: Cannot draw recycled bitmaps异常

android 如何分析java.lang.IllegalArgumentException: Cannot draw recycled bitmaps异常

该类问题的分析,一般是需要找到bitmap对象在那个位置有被recyle,然后再检查代码。
如何定位到是在哪个代码位置有把bitmap 对象recyle,可以在 Bitmap.java的recycle方法中,加log打印出call stack来定位。
 
具体的修改参考如下:
 
 public void recycle() {
        if (!mRecycled) {
            if (nativeRecycle(mNativeBitmap)) {
                // return value indicates whether native pixel object was actually recycled.
                // false indicates that it is still in use at the native level and these
                // objects should not be collected now. They will be collected later when the
                // Bitmap itself is collected.
                mBuffer = null;
                mNinePatchChunk = null;
            }
            mRecycled = true;
           
            log.e("bitmap recyle! ", "this = " + this ,new Throwable("recycle"));


        }
    }
 
然后在抛 java.lang.IllegalArgumentException: Cannot draw recycled bitmaps 异常的地方,也将bitmap 对象打印出来。 抓取复现问题的mobile log 分析。
 
例如如下的log:
 
在main log中搜索“FATAL EXCEPTION”看到发生fatal error exception的 trace如下,对应的bitmap对象为 android.graphics.Bitmap@4218d0c8
01-01 08:03:23.841  2369  2369 D AndroidRuntime: Shutting down VM
01-01 08:03:23.841  2369  2369 W dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x40f979a8)
01-01 08:03:23.856  2369  2369 E AndroidRuntime: FATAL EXCEPTION: main
01-01 08:03:23.856  2369  2369 E AndroidRuntime: java.lang.IllegalArgumentException: Cannot draw recycled bitmapsandroid.graphics.Bitmap@4218d0c8
然后再在main log中搜索关键字 “@4218d0c8” ,就可以看到该对象被recyle的call stack
 
01-01 08:03:22.741  2369  2369 E bitmap recyle! : this = android.graphics.Bitmap@4218d0c8
01-01 08:03:22.741  2369  2369 E bitmap recyle! : java.lang.Throwable: recycle
01-01 08:03:22.741  2369  2369 E bitmap recyle! :  at android.graphics.Bitmap.recycle(Bitmap.java:214)
01-01 08:03:22.741  2369  2369 E bitmap recyle! :  at com.xxx.xxxx.xxxActivity.onDestroy(xxxActivity.java:190)
01-01 08:03:22.741  2369  2369 E bitmap recyle! :  at android.app.Fragment.performDestroy(Fragment.java:1908)
......

android 如何分析java.lang.IllegalArgumentException: Cannot draw recycled bitmaps异常