Jake
Jake

Reputation: 2907

Why am I running out of memory?

I have a 300 x 355 image that is only 50 kb in size. I am trying to decode it with the below code:

Bitmap bm = BitmapFactory.decodeResource(getResources(), drawableId)
    .copy(Bitmap.Config.ARGB_8888, true);

According the logcat 10674000 bytes is trying to be allocated. Why so many? The image is only 50 kb.

Upvotes: 1

Views: 123

Answers (1)

David Manpearl
David Manpearl

Reputation: 12656

300 x 355 = 106500 pixels.

At 4 bytes per pixel, that is 426KB, which is closer to the allocation reported by your LogCat.

The .copy() command in your code will double the memory to 852KB, slightly closer to the 10.6MB reported.

The 50KB number you reported is probably the compressed size of the .jpg or .png file, not the uncompressed Bitmap being used by Android.

Upvotes: 6

Related Questions