安卓:"尝试使用回收的位图"临时位图错误 [英] Android: "trying to use a recycled bitmap" error with temporary Bitmaps

查看:252
本文介绍了安卓:"尝试使用回收的位图"临时位图错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序可以加载非常大的图像。在努力是存储器保守,我试图使用临时位图加载和另一个变换后的最终图像:

My app can load quite large images. In an effort to be memory-conservative, I'm attempting to use a temporary bitmap to load and another for the final image after transformation:

.....
finalBitmap.recycle();
finalBitmap = null;
Bitmap tempBitmap  = BitmapFactory.decodeStream(fin, ...);
finalBitmap = Bitmap.createBitmap(tempBitmap, ....);
imgview.setImageBitmap(finalBitmap);
.....

现在,在这一点上,我们正在与tempBitmap,这是唯一的运输去codeD位图到createBitmap的转换步骤需要完成。所以:

Now, at this point we're done with tempBitmap, which was only needed to transport the decoded Bitmap to the transformation step in createBitmap. So:

.....
tempBitmap.recycle();
tempBitmap = null;
.....

和...它崩溃了,试图用一个循环位图的错误tempBitmap回收利用的具体原因。 tempBitmap没有显示,并且只用于右边有的。

And... it crashes with a "trying to use a recycled bitmap" error specifically because of the recycling of tempBitmap. tempBitmap wasn't displayed and is only used right there.

这是怎么回事错在这里?我应该用finalBitmap贯穿并依靠createBitmap来管理它(的 finalBitmap = Bitmap.createBitmap( finalBitmap ,....)的)?我看不出有什么持续的依赖tempBitmap会有这会导致此类故障。

What's going wrong here? Should I just use "finalBitmap" throughout and rely on createBitmap to manage it (finalBitmap = Bitmap.createBitmap(finalBitmap , ....))? I fail to see what ongoing dependency on tempBitmap there would be that would cause such a failure.

编辑:是,空的分配似乎导致相应的,最后的垃圾收集,但我很迷惑,为什么再循环()上的临时位图是在这种情况下,这样有问题的。我得到的IM pression的createBitmap()持有对它的引用,但为什么,多长时间?

Yes, the null assignment seems to result in the appropriate, eventual garbage collection, but I'm mystified as to why recycle() on a temp Bitmap is so problematic in this case. I get the impression that createBitmap() is holding a reference to it but why, and for how long?

推荐答案

直接从Android的<一个href="http://developer.android.com/reference/android/graphics/Bitmap.html#createBitmap%28android.graphics.Bitmap,%20int,%20int,%20int,%20int%29">documentation:

Straight from the Android documentation:

从源头上的指定子集返回不可变的位图   位图。新位图可以是相同的对象作为源,或副本可   已经作出。

Returns an immutable bitmap from the specified subset of the source bitmap. The new bitmap may be the same object as source, or a copy may have been made.

看来,createBitmap职能必须重新使用您所提供的位图的潜力。如果是这样的话,那么你不应该因为你最终的位图使用它回收临时的位图。有一件事你可以做的是

It seems that the createBitmap functions have the potential to re-use the bitmap that you provided. If that is the case, then you shouldn't recycle the temporary bitmap since your final bitmap is using it. One thing you can do is

if(tempBitmap != finalBitmap) {
   tempBitmap.recycle();
}

这应该只回收tempBitmap当它是不一样的finalBitmap。至少,这似乎是什么文档暗示。

That should only recycle the tempBitmap when it isn't the same as the finalBitmap. At least that seems to be what the documentation is implying.

这篇关于安卓:&QUOT;尝试使用回收的位图&QUOT;临时位图错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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