画布:试图用一个循环位图的android [英] Canvas: trying to use a recycled bitmap android

查看:180
本文介绍了画布:试图用一个循环位图的android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我continously有这个问题,我不知道该怎么办才好。

我用库,当我得到的裁剪图像我将它保存在一个静态变量,并移动到下一个活动。当我在接下来的活动到达时,我引用静态变量来获取位图,并尝试将其缩小。但它给我的错误。

下面是我在做什么。

 公共无效buttonCropClick(查看视图)抛出IOException异常
{
    imageView.setDrawingCacheEnabled(真正的);
    imageView.buildDrawingCache(真正的);
    Snapshot.CroppedBitmap = imageView.getDrawingCache(真正的);
    imageView.setDrawingCacheEnabled(假);
    startActivity(新意图(这一点,RecommendationInfo.class));
}
 

RecommendationInfo 类,我得到了下面的行 Snapshot.CroppedBitmap = imageView.getDrawingCache(真)的位图; 然后我保存此位图中的静态变量我在接下来的活动中引用,并把它传递给下面的函数。

 公共静态位图scaleDown(位图realImage,布尔过滤器){

    浮maxImageSize = HeightToSet;
    流通股比例= Math.min(
            (浮点)maxImageSize / realImage.getWidth()
            (浮点)maxImageSize / realImage.getHeight());
    INT宽度= Math.round((浮动)比* realImage.getWidth());
    INT高= Math.round((浮动)比* realImage.getHeight());

    这里//错误
    位图newBitmap = Bitmap.createScaledBitmap(realImage,宽度,高度,过滤器);
    返回newBitmap;
}
 

我已经打过电话 bitmap.recycle()。为什么会出现这个问题,我能做些什么来解决呢?这是我的logcat。

 三月七号至14日:09:43.713:E / AndroidRuntime(19653):致命异常:主要
3月7日至14日:09:43.713:E / AndroidRuntime(19653):java.lang.RuntimeException的:画布:试图用一个循环位图android.graphics.Bitmap@4059b8b8
3月7日至14日:09:43.713:E / AndroidRuntime(19653):在android.graphics.Canvas.throwIfRecycled(Canvas.java:955)
3月7日至14日:09:43.713:E / AndroidRuntime(19653):在android.graphics.Canvas.drawBitmap(Canvas.java:1012)
3月7日至14日:09:43.713:E / AndroidRuntime(19653):在android.graphics.Bitmap.createBitmap(Bitmap.java:462)
3月7日至14日:09:43.713:E / AndroidRuntime(19653):在android.graphics.Bitmap.createScaledBitmap(Bitmap.java:349)
3月7日至14日:09:43.713:E / AndroidRuntime(19653):在com.example.Libraries.Snapshot.scaleDown(Snapshot.java:42)
3月7日至14日:09:43.713:E / AndroidRuntime(19653):在com.example.androidtestproject.RecommendationInfo.SetRecommendationValues(RecommendationInfo.java:195)
3月7日至14日:09:43.713:E / AndroidRuntime(19653):在com.example.androidtestproject.RecommendationInfo.access $ 5(RecommendationInfo.java:183)
3月7日至14日:09:43.713:E / AndroidRuntime(19653):在com.example.androidtestproject.RecommendationInfo $ 1.onClick(RecommendationInfo.java:154)
3月7日至14日:09:43.713:E / AndroidRuntime(19653):在android.view.View.performClick(View.java:2552)
3月7日至14日:09:43.713:E / AndroidRuntime(19653):在android.view.View $ PerformClick.run(View.java:9229)
3月7日至14日:09:43.713:E / AndroidRuntime(19653):在android.os.Handler.handleCallback(Handler.java:587)
3月7日至14日:09:43.713:E / AndroidRuntime(19653):在android.os.Handler.dispatchMessage(Handler.java:92)
3月7日至14日:09:43.713:E / AndroidRuntime(19653):在android.os.Looper.loop(Looper.java:138)
3月7日至14日:09:43.713:E / AndroidRuntime(19653):在android.app.ActivityThread.main(ActivityThread.java:3701)
3月7日至14日:09:43.713:E / AndroidRuntime(19653):在java.lang.reflect.Method.invokeNative(本机方法)
3月7日至14日:09:43.713:E / AndroidRuntime(19653):在java.lang.reflect.Method.invoke(Method.java:507)
3月7日至14日:09:43.713:E / AndroidRuntime(19653):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:878)
3月7日至14日:09:43.713:E / AndroidRuntime(19653):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:636)
3月7日至14日:09:43.713:E / AndroidRuntime(19653):在dalvik.system.NativeStart.main(本机方法)
3月7日至14日:09:45.515:E / TAG(20039):输入的字符0结束
 

解决方案

复制的位图,你将它传递给静态变量之前。

  Snapshot.CroppedBitmap = imageView.getDrawingCache(真正的);
 

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

这是极有可能是ImageView的是回收其绘图缓存在不再需要时作为很好的做法。通过复制出来,你保持一个参照位ImageView的丢弃。只要它的一个副本,你可以管理自己!

I am continously having this problem and I don't know what to do about it.

I've used this library and when I get the cropped image I save it in a static variable and move to the next activity. When I arrive in the next activity , I reference that static variable to get the bitmap and try to scale it down. But it gives me error.

Here's what I am doing.

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));
}

in the RecommendationInfo class , I get the bitmap in the following line Snapshot.CroppedBitmap = imageView.getDrawingCache(true); then I save this bitmap in the static variable which I reference in next activity and pass it to the following function.

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;
}

I already tried calling bitmap.recycle(). Why am I getting this problem what can I do to solve it ? Here's my logcat.

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 

解决方案

Copy the bitmap before you pass it to the static variable.

Snapshot.CroppedBitmap = imageView.getDrawingCache(true);

to

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

It is very likely that the imageView is recycling its drawing cache when no longer needed as good practice. By copying it out, you keep a reference to the bitmap the ImageView trashed. Just make a copy of it that you can manage yourself!

这篇关于画布:试图用一个循环位图的android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆