Reputation: 41
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
Upvotes: 1
Views: 496
Reputation: 261
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.
Upvotes: 4
Reputation: 18107
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
Upvotes: 0