Image Caching for lots of HD images
I am building an开发者_StackOverflow中文版 "Image Gallery" app that would fetch about 50 - 60 HD images from a site. The site will update the image list thrice a week and so new images will have to be fetched and old images discarded when the app is launched the next time.
What's the best way to cache these images and lazy load them without running into OutOfMemory and SoftReference issues? Lazy loading would be needed because each Image thumbnail has a caption and freezing the thumbnail UI till the image loads is not a good idea.
Also, for lazy loading images - is it really a good design to spawn a thread for each thumbnail and implement a queue to handle the threads? Is there an easier way?
Please help!
Thanks for reading!
You can have a loot at PicHelper from Zwitscher source for a class that does the fetching and storing on the local file system, as well as reading Bitmaps from those files again.
Within the main code you would start an AsyncTask
to fetch the images (in its doInBackground()
method. To prevent OOME I'd put the list of images to fetch in a "queue" and fetch them in serial, rather than parallel. Many handsets out there only have 48MB of heap or less.
As the question is general, so is my attempt to help you. Some ideas: - load images in one separate thread - either store them as files with names on the SD card, or... download them each time the app is run! - put them in a GridView. You can use a message handler (messages from the thread to the main UI thread) to update the GridView - if you decide to store the images on the phone, identify them either by filenames, or store the names in local database.
Actually I have implemented a GridView for reading images on the SD card, and lazy loading images for a list, not exactly what you are trying to do, but it is definitely doable and you can probably find some code on the net.
精彩评论