0

First of all, as a side-note I tested everything with a Moto G. This should work with lower end devices or even higher end (Like Sony Xperia Tablet Z).

I have an app that, in the menu, displays 3 big images. Each image takes the full height of the screen and 1/3 of the screen in width. However, when of the images is tapped the other two leave the screen making the selected one occupy 100% of the screen (in both height and width).

So, given this, I have 3 image views and I load the correspondent image to each one. The images are the exact same screen resolution (1280*720) so I believe I cannot pull any "resize" magic to make them smaller in MB. So everything goes fine when I load the images for the first time and while I keep rotating the device I'll end up in a OutOfMemory Exception (I also have specific images for landscape).

My goal is to be able to display those images without having this exception; but I find it hard because I need three images at the same time having each ~3.5MB (

I tried some methods to reduce the images but hasn't been easy:

  • Have a LruCache that saves each image so I is already loaded into memory and reduce time to read from disk. Max size is [3.5 * 6] (size of each image times the number of possible images: landscape + portrait). Also tried having size only for 3 images but the result is the same. This cache is cleared (evictAll()) when the onLowMemory event is triggered

  • In the onPause event of the activity I get the BitmapDrawable of each ImageView and call recycle() on it. Still leads to OutOfMemory (although less often)

I have two theories that I haven't tried because I don't believe they would help but I'd like to know from you:

  • Use a LruDiskCache. I saw the implementation of it and the main problem will still remain: having the images in memory

  • Use SoftReference<Bitmap>. For what I understand the GC won't touch the references if they are loaded into the memory, right? So in this case they will still in memory and will lead to OutOfMemory, right? And I already recycle each bitmap during onPause so it should be fine, no?

I was considering getting a downscaled version, let's say 75% of the fullscreen, and each image size would end up being ~0.50MB (way less!) but I fear the quality won't be any near the desired.

Miguel Ribeiro
  • 8,057
  • 20
  • 51
  • 74

0 Answers0