I am loading about 200 images from a MySQL database into my android application.
When I try to store them all in Bitmaps and ArrayLists of Bitmaps, I soon get an out of memory exception.
I would like to know what the best way of storing these images is. I could save them all on the sd-card and only save the path to them in my app.
But I also need to always show these images in the pages of a ViewPager (about 8 images are displayed per page). If I fill the ViewPager with images from the ressources, Android and the ViewPager manages the memory-issues itself (it removes non-displayed images from the RAM when it runs out of memory and loads them back, when you scroll back to them)
How can I manage storing so many images without running out of memory?
With the limited memory available you will not be able to have a direct reference to each of the images, otherwise like you say you will run out of memory. These images will take up space in memory as long as there is a reference to your
BitMapobjects. To free up this memory you need to dereference theseBitMapsand or clear theArrayList, this will allow the Garbage Collector to free up the unused memory. Ideally you only want a reference only to the images you can see, or the ones you will see next.You could just look up the 8 images for every page view, or perhaps the the images in the surrounding page views. If the images you are display are much larger than the size they take up then you may want to resize these images on the fly and only store them as the size they appear, this should also help reduce the memory footprint of your images.