Reusing the bitmap variable after recycle

后端 未结 3 912
刺人心
刺人心 2021-01-29 01:15

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 ?

j

相关标签:
3条回答
  • 2021-01-29 01:38

    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.

    0 讨论(0)
  • 2021-01-29 01:47

    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.

    0 讨论(0)
  • 2021-01-29 01:54

    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()
    }
    
    0 讨论(0)
提交回复
热议问题