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

错误提示如题

具体错误内容如下:

08-10 12:21:00.370: E/InputEventReceiver(16613): Exception dispatching input event.
08-10 12:21:00.370: E/MessageQueue-JNI(16613): Exception in MessageQueue callback: handleReceiveCallback
08-10 12:21:00.377: E/MessageQueue-JNI(16613): java.lang.RuntimeException: Canvas: trying to use a recycled bitmap android.graphics.Bitmap@41bd0800
08-10 12:21:00.377: E/MessageQueue-JNI(16613): 	at android.graphics.Canvas.throwIfRecycled(Canvas.java:1026)
08-10 12:21:00.377: E/MessageQueue-JNI(16613): 	at android.graphics.Canvas.drawBitmap(Canvas.java:1096)
08-10 12:21:00.377: E/MessageQueue-JNI(16613): 	at android.graphics.Bitmap.createBitmap(Bitmap.java:604)
08-10 12:21:00.377: E/MessageQueue-JNI(16613): 	at com.android.francis.gesture.listener.CustomGestureListener.onFling(CustomGestureListener.java:55)
08-10 12:21:00.377: E/MessageQueue-JNI(16613): 	at android.view.GestureDetector.onTouchEvent(GestureDetector.java:1387)
08-10 12:21:00.377: E/MessageQueue-JNI(16613): 	at com.android.francis.gesture.MainActivity.onTouchEvent(MainActivity.java:31)
08-10 12:21:00.377: E/MessageQueue-JNI(16613): 	at android.app.Activity.dispatchTouchEvent(Activity.java:2471)
08-10 12:21:00.377: E/MessageQueue-JNI(16613): 	at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2061)
08-10 12:21:00.377: E/MessageQueue-JNI(16613): 	at android.view.View.dispatchPointerEvent(View.java:7525)
08-10 12:21:00.377: E/MessageQueue-JNI(16613): 	at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:3370)
08-10 12:21:00.377: E/MessageQueue-JNI(16613): 	at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:3302)
08-10 12:21:00.377: E/MessageQueue-JNI(16613): 	at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:4394)
08-10 12:21:00.377: E/MessageQueue-JNI(16613): 	at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:4372)
08-10 12:21:00.377: E/MessageQueue-JNI(16613): 	at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:4476)
08-10 12:21:00.377: E/MessageQueue-JNI(16613): 	at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:171)
08-10 12:21:00.377: E/MessageQueue-JNI(16613): 	at android.os.MessageQueue.nativePollOnce(Native Method)
08-10 12:21:00.377: E/MessageQueue-JNI(16613): 	at android.os.MessageQueue.next(MessageQueue.java:125)
08-10 12:21:00.377: E/MessageQueue-JNI(16613): 	at android.os.Looper.loop(Looper.java:124)
08-10 12:21:00.377: E/MessageQueue-JNI(16613): 	at android.app.ActivityThread.main(ActivityThread.java:4895)
08-10 12:21:00.377: E/MessageQueue-JNI(16613): 	at java.lang.reflect.Method.invokeNative(Native Method)
08-10 12:21:00.377: E/MessageQueue-JNI(16613): 	at java.lang.reflect.Method.invoke(Method.java:511)
08-10 12:21:00.377: E/MessageQueue-JNI(16613): 	at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:994)
08-10 12:21:00.377: E/MessageQueue-JNI(16613): 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:761)
08-10 12:21:00.377: E/MessageQueue-JNI(16613): 	at dalvik.system.NativeStart.main(Native Method)

好长的一段提示信息,根据提示信息我们可以定位到错误位置:手动释放Bitmap对象中的图片资源造成的。

看程序代码:

//		判断图片资源是否被回收
		if (drawable != null && !drawable.getBitmap().isRecycled()) {
//			强制回收图片资源
			drawable.getBitmap().recycle();
		}

既然是手动释放Bitmap对象中的资源文件造成的错误,第一个解决方案就是:

我们就不要自己来做这个释放操作了,留给程序自己做吧,上面这段代码全部注释掉

测试结果OK,成功的。

第二个解决方案,看代码:

//		判断图片资源是否被回收
		if (drawable != null && !drawable.getBitmap().isRecycled()) {
//			强制回收图片资源
//			drawable.getBitmap().recycle();
			Bitmap bm = drawable.getBitmap();
			bm = null;
		}

获取当前的Bitmap对象,然后赋予null值,程序测试OK。

如果还有其他Bug、高见,请各位留言。

为嘛手动释放就错误,我也不清楚,知道的大侠也可以留言。

你可能感兴趣的:(android,bitmap,recycled)