Android如何试图使用回收的位图,而不是在我的代码中?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (2)
  • 关注 (0)
  • 查看 (62)

我从来没有在我的代码中的任何地方使用过Bitmap; 我对图像的唯一引用是在我的layout.xml中:

<ImageView android:id="@+id/splashScreen"
  android:layout_height="fill_parent"
  android:layout_width="fill_parent"
  android:src="@drawable/splashscreen"
  android:scaleType="fitXY"
/>

我能做些什么吗。我的代码如下:

java.lang.RuntimeException: Canvas: trying to use a recycled bitmap android.graphics.Bitmap@4721ec18
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:923)
at android.view.View.draw(View.java:6761)
at android.view.ViewGroup.drawChild(ViewGroup.java:1663)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1390)
at android.view.View.buildDrawingCache(View.java:6517)
at android.view.View.getDrawingCache(View.java:6305)
at android.view.ViewGroup.drawChild(ViewGroup.java:1588)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1390)
at android.view.ViewGroup.drawChild(ViewGroup.java:1661)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1409)
at android.view.View.draw(View.java:6764)
at android.widget.FrameLayout.draw(FrameLayout.java:352)
at android.view.ViewGroup.drawChild(ViewGroup.java:1663)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1390)
at android.view.ViewGroup.drawChild(ViewGroup.java:1661)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1390)
at android.view.ViewGroup.drawChild(ViewGroup.java:1661)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1390)
at android.view.View.draw(View.java:6764)
at android.widget.FrameLayout.draw(FrameLayout.java:352)
at android.view.ViewGroup.drawChild(ViewGroup.java:1663)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1390)
at android.view.ViewGroup.drawChild(ViewGroup.java:1661)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1390)
at android.view.View.draw(View.java:6764)
at android.widget.FrameLayout.draw(FrameLayout.java:352)
at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:1887)
at android.view.ViewRoot.draw(ViewRoot.java:1432)
at android.view.ViewRoot.performTraversals(ViewRoot.java:1167)
at android.view.ViewRoot.handleMessage(ViewRoot.java:1764)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:143)
at android.app.ActivityThread.main(ActivityThread.java:5068)
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:858)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
at dalvik.system.NativeStart.main(Native Method)
提问于
用户回答回答于

我认为你可以在onResume方法中重新创建你的位图,但是在onCreate或者onResume中使用一个标志来创建位图,而不是两者都一样。

用户回答回答于

当你尝试回收或使用回收的位图时,会发生此异常。你不应该从现有代码中删除bitmap.recycle()函数,它可能会导致内存不足错误。你可以试试下面的代码来解决这个问题:

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

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

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

                    log("bitmap is already recycled");
                }

                bitmapDrawable = null;

扫码关注云+社区