Reusing the bitmap variable after recycle

半城伤御伤魂 提交于 2019-12-31 07:29:20

问题


I have recycled the bitmap variable and when I again tried to use the same variable I have some strange errors with reference to recycle. Any solution to this issue ?

java code:

image1.recycle(); image1=null;

LogCat:

05-29 11:35:42.139: E/AndroidRuntime(695): java.lang.RuntimeException: Canvas: trying to use a recycled bitmap android.graphics.Bitmap@2bbad018
05-29 11:35:42.139: E/AndroidRuntime(695):  at android.graphics.Canvas.throwIfRecycled(Canvas.java:1038)
05-29 11:35:42.139: E/AndroidRuntime(695):  at android.graphics.Canvas.drawBitmap(Canvas.java:1078)

回答1:


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.

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

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.




回答2:


Once you recycle the bitmap its memory is freed. That's mean that bitmaps data are gone from the memory. If you want to use again the same variable you have to decode the Bitmap againg .

if (image1 == null || image1.isRecycled()) {
    image1 = BitmapFactory.decodeStream()
}



回答3:


You can have check before recycling the bitmap, as like :

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

Here img is bitmap.

Try this type error is sloved.



来源:https://stackoverflow.com/questions/16806782/reusing-the-bitmap-variable-after-recycle

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!