1

I've a problem concerning loading images from the internet with bitmapfactory.decodestream. For example this image: https://portal.apprenticexm.nl/appportal/public/apps/1/media/807162_88149365.jpg

It's just over 100 KB in size, but the error on bitmapfactory.decodestream says that I try to allocate more then 20 MB: Caused by: java.lang.OutOfMemoryError: bitmap size exceeds VM budget(Heap Size=8007KB, Allocated=3662KB, Bitmap Size=23756KB)

I hope someone is able to shed some light on this problem.

Best, Pieter

trincot
  • 317,000
  • 35
  • 244
  • 286
  • lock at this answer: http://stackoverflow.com/questions/477572/android-strange-out-of-memory-issue-while-loading-an-image-to-a-bitmap-object/823966#823966 – Pasha Aug 29 '12 at 07:39
  • I've seen those answers, but could you explain how it can happen that a 100KB image blows up to more then 20 MB? – user1632374 Aug 29 '12 at 07:56

2 Answers2

4

As Alex Orlov stated: "the size on disk has nothing to do with the size of bitmap in memory. In the first case the image is compressed, bitmap, on the other hand, is just a raw set of pixels." However even considering it is stored as a raw image, 20MB is stil way too much. If we consider 4B per pixel, that would be 5Mpix image, but that one you have posted is definittely smaller.

I had similar problem: I was loading an image that has 8MB in raw format, however, 32MB were allocated for it. I later found out this was caused by dpi scaling. If you have your image in "drawable" folder, it will be automatically scaled according to current screen dpi. I have an XHDPI screen, so my image was scaled 2x horizontally and 2x vertically, that's why it took 4x more memory.

You can find more about how dpi works here: http://developer.android.com/guide/practices/screens_support.html

If you don't want to use this automatic android feature and scale images yourself, simply rename your "drawable" folder to "drawable-nodpi". All images in "drawable" folder tagged as "nodpi" will be loaded as they are.

Matúš Kotry
  • 261
  • 2
  • 5
0

The size on disk has nothing to do with the size of bitmap in memory. In the first case the image is compressed, bitmap, on the other hand, is just a raw set of pixels. You must also take into consideration such things as image config. If I remember right, jpg doesn't support alpha channel, and therefore is more lightweight than android's default configuration of ARGB_8888

You can control how to load the image with BitmapFactory.Options

Alex Orlov
  • 18,077
  • 7
  • 55
  • 44
  • But if I downsample the images to at max the screen size, I should never ran into problems (except for the total of number images used)? – user1632374 Aug 29 '12 at 11:38