android Download hundreds of images and store them to be used on revisit or when the internet is not there

StackOverflow https://stackoverflow.com/questions/12571869

  •  03-07-2021
  •  | 
  •  

Domanda

I have a listview and each listview item when clicked opens a gallery (conating 30 + images ) to be downloaded (using the urls).I don't want to re - download the images on next visit and also want the images to be present when i click on list in offline mode.

I went through several links in SO and am a bit confused on which approach to follow:

  • 1> Use bytearray and store the images in sqlite db.
  • 2> use context -> getExternalCacheDir() to store it on the external memory (this wont work on devices with no external memory.)
  • 3> Use SoftReferences as suggested in following link

http://android-developers.blogspot.in/2010/07/multithreading-for-performance.html

Please help to guide me on the best approach to handle around 1000 + images which works in offline mode as well.

È stato utile?

Soluzione

1000 images.....I sure hope they're thumbnails or you're going to end using a lot of storage space.

You should really only cache what you need and what images are used repeatedly. Both options 1 and 2 are dependant on the phone even having storage space, if there's no space then you won't be caching much to disk. 3 won't store images permanently, if the app is put in the background and gets killed and so will the cache.

It's hard to say what the correct decision here is without knowing the inner workings and requirements of the app.

Just going by what you want then I would say go with several solutions

If there's external storage write to that, if not use internal storage, if there's not enough space internally use the soft references.

I have always found that out of the box caching solutions never fit quite right with my apps and wind up writing my own cache. Sorry I can't give a better answer.

PS. If you're not careful with 3 you might download too much and run into an OutOfMemoryException.

PPS. 3 can also throw a RejectedExecutionException which is caused by too many async tasks being started at once (eg scrolling quickly through a listview that gets an image via asynctask)

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top