Tom
Tom

Reputation: 25

Background image runs out of memory

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.

Upvotes: 0

Views: 453

Answers (2)

Drilon Blakqori
Drilon Blakqori

Reputation: 2826

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.

Upvotes: 0

Omid Heshmatinia
Omid Heshmatinia

Reputation: 5236

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

Upvotes: 2

Related Questions