skip to Main Content

Okay, so I have this really weird problem and I will explain it with an example.

Lets say I have 2 activities: ActivityOne, ActivityTwo. When pressing a button in ActivityOne it opens ActivityTwo (intent)

I have 2 background images in @drawables: onebg (size=31k), twobg (size=30k)

onebg is the background in ActivityOne.
When trying to make twobg the background of ActivityTwo, it crashes with the Out of Memory error.

However, when setting onebg as the background for both ActivityOne and ActivityTwo, it works. Haven’t tried setting twobg for both of them but I guess it will most likely work.

Both onebg and twobg are made by me in photoshop, they are the same type and everything, just different size because of different text on it.

2

Answers


  1. You should use an image loading library for loading big images. I’d recommend Glide https://github.com/bumptech/glide, since it’s also mentioned here Android developers – Loading Large Bitmaps Efficiently. You should also read this for more information about loading large bitmaps.

    Login or Signup to reply.
  2. I think you didn’t understand the heap allocation of bitmaps correctly.

    The size which each bitmap allocate in heap is not determined by its size !!! it’s determined by its dimension !

    Lets have an example:

    you have a bitmap with 30kb size and dimension of 1000*500 pixel. the amount of memory is determined this way:

    1000 * 500 * 4 (its because each pixel should contain ARGB
    informations)

    So on devices with low heap you would have problem with such image sizes.

    Also pay attention to @Drilon hint.
    You also should pay attention to Memory leak too.

    Fore more information please refer to this link from google

    Loading Large Bitmaps Efficiently

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search