java.lang.OutOfMemoryError: bitmap size exceeds VM budget - Android

前端 未结 13 1783
醉话见心
醉话见心 2020-11-22 01:32

I developed an application that uses lots of images on Android.

The app runs once, fills the information on the screen (Layouts, Listviews,

13条回答
  •  爱一瞬间的悲伤
    2020-11-22 02:11

    I too am frustrated by the outofmemory bug. And yes, I too found that this error pops up a lot when scaling images. At first I tried creating image sizes for all densities, but I found this substantially increased the size of my app. So I'm now just using one image for all densities and scaling my images.

    My application would throw an outofmemory error whenever the user went from one activity to another. Setting my drawables to null and calling System.gc() didn't work, neither did recycling my bitmapDrawables with getBitMap().recycle(). Android would continue to throw the outofmemory error with the first approach, and it would throw a canvas error message whenever it tried using a recycled bitmap with the second approach.

    I took an even third approach. I set all views to null and the background to black. I do this cleanup in my onStop() method. This is the method that gets called as soon as the activity is no longer visible. If you do it in the onPause() method, users will see a black background. Not ideal. As for doing it in the onDestroy() method, there is no guarantee that it will get called.

    To prevent a black screen from occurring if the user presses the back button on the device, I reload the activity in the onRestart() method by calling the startActivity(getIntent()) and then finish() methods.

    Note: it's not really necessary to change the background to black.

提交回复
热议问题