I am working on an android application. I have an activity that contains a list view.
The list view has lots of items and each contains a bitmap image.
Because the list is very big and contains a lot of images it is giving and out of memory error on some old devices when loading the bitmaps.
So I am looking for a way to do the following for old devices:
- Load the bitmaps for the items that are in the view (or near it)
- When the user scrolls down I load the bitmaps that should appear to him and remove the bitmaps that are now hidden.
- So at anytime only the images that are in the view (or near it) are loaded in the Heap while the others are removed when they are no longer in the view.
Is there a way to do it?
Additional information:
I have followed every instructions and code on how to load Bitmap images efficiently (on the android documentation and on any site that I could find), I also made sure there is no memory leaks, I recycle every bitmap and make sure the GC works correctly and finally, on old devices I made the resolution of the images the smallest possible. But the size of the list is causing the out of memory to still appear on some devices.
So I am looking to know if I can do the above proposed solution.
Thanks a lot for any help
You should consider looking at Sample Gallery App from Google I/O 2012. Everything you need is implemented there.
Video from Google I/O about gallery app
Source code is available on Google Code and it’s an exclellent example of building a Gallery. You should also consider looking at Romain Guy’s presentation (beginning) where he explains performance issues with
ListViewand teaches to use adapters efficiently. (ViewHolder pattern and reusing views).Here’s a RecycleListener for GridView (Available from API level 1). A RecyclerListener is used to receive a notification whenever a View is placed inside the RecycleBin’s scrap heap. This listener is used to free resources associated to Views placed in the RecycleBin.