0

My app uses 29 images in 2 different classes (and layouts). In 1 layout there is 1 image view that changes between at most 25 of the images, and then the other layout is a scroll view that contains all images where at most 3 or 4 are visible on screen at a time. Previously I had the images saved as .jpg and each is 720*540 and around 200-300kb in size. My app has had out of memory issues after going between my 2 layouts that are using the images; it is using over 100 mb. I did some research on bitmaps and tried them for my images, but they didn't help much because I still get out of memory issues and they increased the .apk size by almost 3 times. What is the most preferred way to load images, is it as a drawable from a .jpg or from a bitmap or some other way? Any suggestions would be appreciated.

6
  • I am a little confused. Why bidmap increases the apk files? We decode them in the java code, right? This link could be helpful for you. developer.android.com/training/displaying-bitmaps/…
    – Anson Yao
    Aug 1, 2013 at 0:25
  • bitmap files are much larger, bitmap files are uncompressed and jpg files are compressed
    – ez4nick
    Aug 1, 2013 at 0:31
  • I know bitmap is uncompressed. But we do not have them in the apk file, right? We use BitmapFactory to decode them only when we run the app. The apk file only have jpg instead of bitmaps
    – Anson Yao
    Aug 1, 2013 at 1:50
  • decode what you need and then call recycle() to get memory back if you are not displaying them.
    – Anson Yao
    Aug 1, 2013 at 1:50
  • the bitmap files need to be in the .apk package otherwise the app wouldn't work, they are much larger then a .jpg file thus making the .apk file size much larger
    – ez4nick
    Aug 1, 2013 at 1:55

2 Answers 2

2

when the bitmap is in memory, it's uncompressed. the original format doesn't matter. you can count on each image taking about width * height * 4 bytes of memory. e.g., each of your 720x540 images will take ~1.5MB of memory.

it does not matter where you load them from, at least as far as the amount of memory required.

now read this,

http://developer.android.com/training/displaying-bitmaps/cache-bitmap.html

3
  • ok so if I understand correctly the link is suggesting to cache all images and only load the images that are currently on the screen?
    – ez4nick
    Aug 1, 2013 at 0:31
  • 1
    yes, all images should come from the cache. best is to put a wrapper around that class that either gets from the cache or loads from the source and inserts into the cache. and yes, typically you'd have a view that uses an adapter. in the adapter's "get view" method, you load the image ... meaning, it either comes from the cache or gets loaded from the source. Aug 1, 2013 at 0:51
  • ok thanks for the info. im sure after more research I will be able to accomplish this
    – ez4nick
    Aug 1, 2013 at 1:14
0

You can read the source code of system Gallery, then you can totally understand how to operate bitmap. When you get an image at first time, you have to decode the original file, and save it to cache file. Then for the next time, you just decode the cache file, which is faster. Your memory is limited. Don't expect to store all your bitmaps, just the bitmap that you wanna show on the screen. The size of thumbnail is much smaller than full image. So there is no problem if you wanna put 8 ~ 10 thumbnails on screen. When users click the thumbnail to browse the full image, you can load the full image into memory, and recycle other bitmaps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.