【问题标题】:Canvas: trying to use a recycled bitmap android画布:尝试使用回收的位图android
【发布时间】:2013-07-12 04:07:21
【问题描述】:

我一直有这个问题,我不知道该怎么办。

我使用了this 库,当我得到裁剪后的图像时,我将它保存在一个静态变量中并移至下一个活动。当我到达下一个活动时,我引用该静态变量来获取位图并尝试按比例缩小它。但它给了我错误。

这就是我正在做的事情。

public void buttonCropClick(View view) throws IOException
{
    imageView.setDrawingCacheEnabled(true);
    imageView.buildDrawingCache(true);
    Snapshot.CroppedBitmap = imageView.getDrawingCache(true);
    imageView.setDrawingCacheEnabled(false);
    startActivity(new Intent(this,RecommendationInfo.class));
}

RecommendationInfo 类中,我在以下行Snapshot.CroppedBitmap = imageView.getDrawingCache(true); 中获取位图,然后将此位图保存在我在下一个活动中引用的静态变量中,并将其传递给以下函数。

public static Bitmap scaleDown(Bitmap realImage,boolean filter) {

    float maxImageSize = HeightToSet;
    float ratio = Math.min(
            (float) maxImageSize / realImage.getWidth(),
            (float) maxImageSize / realImage.getHeight());
    int width = Math.round((float) ratio * realImage.getWidth());
    int height = Math.round((float) ratio * realImage.getHeight());

    // Error here
    Bitmap newBitmap = Bitmap.createScaledBitmap(realImage, width,height, filter);
    return newBitmap;
}

我已经试过打电话给bitmap.recycle()。为什么我会遇到这个问题,我该怎么做才能解决它?这是我的日志。

07-14 03:09:43.713: E/AndroidRuntime(19653): FATAL EXCEPTION: main
07-14 03:09:43.713: E/AndroidRuntime(19653): java.lang.RuntimeException: Canvas: trying to use a recycled bitmap android.graphics.Bitmap@4059b8b8
07-14 03:09:43.713: E/AndroidRuntime(19653):    at android.graphics.Canvas.throwIfRecycled(Canvas.java:955)
07-14 03:09:43.713: E/AndroidRuntime(19653):    at android.graphics.Canvas.drawBitmap(Canvas.java:1012)
07-14 03:09:43.713: E/AndroidRuntime(19653):    at android.graphics.Bitmap.createBitmap(Bitmap.java:462)
07-14 03:09:43.713: E/AndroidRuntime(19653):    at android.graphics.Bitmap.createScaledBitmap(Bitmap.java:349)
07-14 03:09:43.713: E/AndroidRuntime(19653):    at com.example.Libraries.Snapshot.scaleDown(Snapshot.java:42)
07-14 03:09:43.713: E/AndroidRuntime(19653):    at com.example.androidtestproject.RecommendationInfo.SetRecommendationValues(RecommendationInfo.java:195)
07-14 03:09:43.713: E/AndroidRuntime(19653):    at com.example.androidtestproject.RecommendationInfo.access$5(RecommendationInfo.java:183)
07-14 03:09:43.713: E/AndroidRuntime(19653):    at com.example.androidtestproject.RecommendationInfo$1.onClick(RecommendationInfo.java:154)
07-14 03:09:43.713: E/AndroidRuntime(19653):    at android.view.View.performClick(View.java:2552)
07-14 03:09:43.713: E/AndroidRuntime(19653):    at android.view.View$PerformClick.run(View.java:9229)
07-14 03:09:43.713: E/AndroidRuntime(19653):    at android.os.Handler.handleCallback(Handler.java:587)
07-14 03:09:43.713: E/AndroidRuntime(19653):    at android.os.Handler.dispatchMessage(Handler.java:92)
07-14 03:09:43.713: E/AndroidRuntime(19653):    at android.os.Looper.loop(Looper.java:138)
07-14 03:09:43.713: E/AndroidRuntime(19653):    at android.app.ActivityThread.main(ActivityThread.java:3701)
07-14 03:09:43.713: E/AndroidRuntime(19653):    at java.lang.reflect.Method.invokeNative(Native Method)
07-14 03:09:43.713: E/AndroidRuntime(19653):    at java.lang.reflect.Method.invoke(Method.java:507)
07-14 03:09:43.713: E/AndroidRuntime(19653):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:878)
07-14 03:09:43.713: E/AndroidRuntime(19653):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:636)
07-14 03:09:43.713: E/AndroidRuntime(19653):    at dalvik.system.NativeStart.main(Native Method)
07-14 03:09:45.515: E/TAG(20039): End of input at character 0 of 

【问题讨论】:

  • 它给了你什么错误?
  • 我已经更新了问题,请检查
  • 您的位图在某处被回收,回收后无法调用该位图。请在开始其他活动之前查看您是否回收位图。
  • createScaledImage 之后添加if (newBitmap != realImage) {realImage.recycle(); realImage = newBitmap ;} System.gc();,正如k0sh 提到的那样.. 确保在将其处理到下一个活动之前首先不要回收位图。
  • 仍然遇到同样的错误。我试过你的代码

标签: android bitmap


【解决方案1】:

在将位图传递给静态变量之前复制位图。

Snapshot.CroppedBitmap = imageView.getDrawingCache(true);

Snapshot.CroppedBitmap = Bitmap.createBitmap(imageView.getDrawingCache(true));

当不再需要时,imageView 很可能正在回收其绘图缓存作为良好做法。通过将其复制出来,您可以保留对 ImageView 丢弃的位图的引用。只需复制一份您可以自己管理的内容!

【讨论】:

【解决方案2】:

你需要先复制源图片:

Bitmap bitmap = webview.getDrawingCache();
if (bitmap != null) {
        Snapshot.CroppedBitmap.setImageBitmap(bitmap.copy(bitmap.getConfig(), false));
}
  • 使用 createBitmap 不一定会创建源位图的副本,导致同样的错误。

【讨论】:

  • java.lang.IllegalStateException: 无法复制回收的位图
【解决方案3】:

在我的情况下,我必须将以前的位图复制到新位图并将 mutable 设置为 true。就是它。

Bitmap copy = previousBitmap.copy(Bitmap.Config.ARGB_8888, true);

现在我可以对位图执行新的操作了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-10-01
    • 1970-01-01
    • 2015-09-21
    • 2011-03-03
    • 2015-12-04
    • 1970-01-01
    • 2011-12-20
    相关资源
    最近更新 更多