1

I need to load 20+ images on top of each other to form an interactive map where each layer can be turned on and off.

I decided to use a LayerDrawable for this, but I keep getting an OutOfMemoryError. Even after I set Large Heap to true, it'll be able to load about 8 images but any more than that it'll still throw the error.

Is there a way to load a lot of images into a LayerDrawable without getting that error, or is there a better way to do this?

user
  • 86,916
  • 18
  • 197
  • 190
ZhaoYiLi
  • 173
  • 1
  • 10
  • 1
    How big are those images? – user Jun 24 '13 at 16:08
  • @Luksprog The smallest one is 5.66kb, and the largest is 2.74mb – ZhaoYiLi Jun 24 '13 at 16:13
  • 1
    You can't load all those images in memory as there's no memory for them. I would make a custom view in which I would load the images on demand(assuming that not all of them are visible somehow). Also have a look at http://developer.android.com/training/displaying-bitmaps/index.html – user Jun 24 '13 at 16:23
  • @Luksprog Thank you, that link was very helpful. I was able to get all of the images to load without a problem. – ZhaoYiLi Jun 25 '13 at 17:02
  • the file size doesn't say much about how much it will take in memory. you should have provided information about the resolution and color depth of the image. – android developer Jun 25 '13 at 17:32

3 Answers3

1

Thanks to Luksprog's link I was able to fix the problem.

I used inSampleSize to load a smaller version of each of the images before adding them to the Drawable[]. This cut down the amount of memory used by a lot.

ZhaoYiLi
  • 173
  • 1
  • 10
0

Somthing like that ?

Resources r = getResources();
Drawable[] layers = new Drawable[20];
layers[0] = r.getDrawable(R.drawable.01);
layers[1] = r.getDrawable(R.drawable.02);
etc[...]
LayerDrawable layerDrawable = new LayerDrawable(layers);
testimage.setImageDrawable(layerDrawable);
marshallino16
  • 2,645
  • 15
  • 29
0

using the large heap flag works only on honeycomb and above, and it doesn't promise you anything about how much memory you will get. you could get the same amount as before.

if you wish to load a lot of high quality images, either you scale them down to max of the screen, or use a non-java solution (for example openGL/andengine/lbgdx to show the content) .

if you choose a java solution, you should know that the bare minimum of devices' heap for apps is 16MB . i wish it was larger starting with some sort of android API, but this is not something that you can assume (though i've never seen a new device with such a limitation yet) .

in general , you can assume that your app always has enough memory to hold a bitmap that has the entire screen. you might even assume it always has double than this, but that's it.

if you choose the native other solutions, you are only limited to the device RAM memory, but you are on your own for releasing the memory.

do note that no matter what solution you choose, you are still limited to the amount of memory the device has.

android developer
  • 114,585
  • 152
  • 739
  • 1,270