Android trying to use a recycled bitmap, not in my code

前端 未结 6 1347
滥情空心
滥情空心 2020-12-30 00:56

I get this stacktrace from the Market developer console every once in a while; I can\'t find any way to repro the error. It\'s happening while displaying a splashscreen Imag

相关标签:
6条回答
  • 2020-12-30 00:57

    This exception occurs when you try to recycle or use a recycled bitmap. You should not remove bitmap.recycle() function from your existing code, it may cause Out Of Memory Error. You can try this below code to fix the issue.

    BitmapDrawable bitmapDrawable = ((BitmapDrawable) profileWallpaper.getDrawable());
    
                    if (null != bitmapDrawable && !bitmapDrawable.getBitmap().isRecycled()) {
    
                        bitmapDrawable.getBitmap().recycle();
                    } else {
    
                        log("bitmap is already recycled");
                    }
    
                    bitmapDrawable = null;
    

    Note: profileWallpaper is your imageView object

    0 讨论(0)
  • 2020-12-30 01:00

    If you recycle your bitmap somewhere in your code, you should be sure that, when the app restart, your bitmap would be recreated. When you pass through the onCreate, no issues, but when the app is in pause and restarted it will not pass through the onCreate again, just through the onResume. I think you could recreate your bitmaps in the onResume method, but use a flag to create the bitmap in the onCreate or in the onResume, not both (onCreate and onResume are called together when the app starts and you could waste CPU cycles).

    0 讨论(0)
  • 2020-12-30 01:00

    Just solved the issue with similar crash log. In my case it was just second ProgressBar inside the fragment. I had two lists inside it and ProgressBars for each of them. Removing the second one solved the problem. I can only guess that they were using the same bitmap resource for background or animation...

    I've spent a lot of time to understand its reason, and it was just a standart ProgressBar! :/ Hope this help someone to save the time and peace of mind.

    0 讨论(0)
  • 2020-12-30 01:03

    You should not use same image resources for different Activities while one of them is recycling its bitmap.

    If you have this situation, changed the image resource of one of those Activities.

    0 讨论(0)
  • 2020-12-30 01:08

    I was having the same Exception that is not coming from my application and found out that it is not related to AdMob.

    The issue arises when you recycle a BitmapDrawable resource that was set through XML to your ImageView.

    While resuming, android doesn't decode the image again whether it is recycled or not, therefore, you are getting Canvas: trying to use a recycled bitmap.

    The way to get solve this issue is to reload the corresponding BitmapDrawable at onPause of your Activity so that android would not complain about the bitmap being recyced.

    Here is how to do it:

    Bitmap selectImage = BitmapFactory.decodeResource(getResources(), R.drawable.your_bitmap_drawable);
    yourImageView.setImageBitmap(selectImage);
    

    Edit: I realized simply setting Drawable of yourImageView to null also prevents this error. Just don't leave a Drawable that has a recycled Bitmap. Example code:

    yourImageView.setImageDrawable(null);
    
    0 讨论(0)
  • 2020-12-30 01:10

    Have you tried to lower the ress of the image? It happened to me lots of times. If the problem continues after rezise the image, it means that the problem is in somewere else... I would look in the on create methode from your main activity or in the splashscreen code.

    sorry my crappy english :)

    C ya!

    0 讨论(0)
提交回复
热议问题