试图使用回收的位图android.graphics.Bitmap

时间:2013-01-03 09:23:24

标签: android

我在activity A中显示一个.gif文件。当用户按下一个按钮时,用户移动到activity b并且同时显示.gif但我在android.graphics.Bitmap上“尝试使用回收的位图activity b”此错误。

我在sd卡和onStop()上显示.gif我在imageview activity a设置了null以及activity A,但我不是能够解决这个问题。

请在这种情况下提供帮助。

// 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)

3 个答案:

答案 0 :(得分:14)

您可以在回收位图之前进行检查,如:

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

此处 img 是位图。  尝试此类错误已解决。

答案 1 :(得分:0)

尝试在您的代码或您使用的任何库中查找方法recycle()的用法参考。它表明有人正在回收仍在使用的位图,这是不好的。当您完全确定不再需要给定的位图时,您只能安全地调用recycle()方法。

当您处理位图缓存时,这通常是一个常见问题,不确定是否属于您的情况。

答案 2 :(得分:0)

此处已提供解决方案:Canvas: trying to use a recycled bitmap android.graphics.Bitmap in android

这应该工作。否则,请查看链接以获取更多解决方案

if (mBitmap != null && !mBitmap.isRecycled()) {
    mBitmap.recycle();
    mBitmap = null; 
}