OutOfMemoryError when loading activities

后端 未结 3 1127
挽巷
挽巷 2021-01-03 09:26

I have a quite simple activity like this :

public class SurvivalActivity extends Activity {
    private static final String KEY_LAYOUT_ID = \"SurvivalLayoutI         


        
3条回答
  •  失恋的感觉
    2021-01-03 10:13

    It might just be that the bitmap you're putting in the ImageView is very large. Here's some things you can try:

    Ditch the xml layout and just inflate and display the problematic ImageView or ImageViews and see if this way you're getting the error.

    Also check each of your images to see what size they actually have. The size they'll take in memory is roughly 4*width*height (in bytes, where width and height are the image's size in pixels).

    Since your Bitmap instances are taking up memory on the heap, I'm imagining you're using Android 3 or above. In these versions that's where Bitmap instances go (just like any other Java instance). In Android 2.x and below, Bitmap instances were allocated in some offheap memory. Knowing this, make sure that that high heap usage you're getting is indeed from the images, and not from some other instances that are created a lot. Try debugging your constructors and see who calls what when the app starts/runs.

    As per Bicou's observations, you might want to manually load your images via BitmapFactory, and in doing so maybe downsample them (via the Options object). Also, try keeping a counter of how much image bytes in memory you total by incrementing with 4*width*height of each image you're actually loading into a Bitmap Object.

提交回复
热议问题