0

My app shows images to user one after one. The images are downloaded from server. As I do not want the user to wait for the images to be downloaded I cached those in a local file system (say new 50 images) .

Implementation: from onCreate method start a AsyncTask that will keep in downloading images in background. Is AsyncTask best for this use case?I do not want to use a Service as I do not want the download to happen continuously

Is AsyncTask best for this purpose?

jyomin
  • 1,957
  • 2
  • 11
  • 27
user93796
  • 18,749
  • 31
  • 94
  • 150

3 Answers3

2

I would suggest using an existing library, such as Picasso (by Square) or Volley (by Google).

Picasso is especially easy to implement and will work excellently for your purpose, and is as easy as:

Picasso.with(context).load("http://i.imgur.com/DvpvklR.png").into(imageView);

You won't have to concern yourself with AsyncTasks or AsyncTaskLoaders as the library takes all of this out of your hands, caching both images in memory and on the disk.

If you insist on building your own solution, I suggest looking around for related questions as there are many things to bear in mind:

  • AsyncTasks may be ill fitted as they are directly coupled with a single activity; you'll need a Loader or a mechanism similar to Loaders to deliver results to whichever activity wants an image.
  • On older devices, bitmaps reside in a special region of memory that isn't managed by the GC; you'll have to dispose of bitmaps manually when you're done with them.
  • Bitmaps use a significant amount of memory and need to be carefully managed to avoid OOMEs, such as downsampling them when loading and storing in memory inside an LruCache.
  • Beware of managing the size of the image cache directory when writing images to disk.
Paul Lammertsma
  • 37,593
  • 16
  • 136
  • 187
1

Yes, you can use Asynctask for this purpose. And using one of the apis of Asynctask "onProgressUpdate(), you can in parallel update the UI as your images are getting downloaded.

Sushil
  • 8,250
  • 3
  • 39
  • 71
  • You'll need to also check for nullity of the UI once the processing returns back to the UI thread too. The activity could have been sent to the background while the ASyncTask was doing the heavy work. – Ben Pearson Mar 05 '14 at 08:44
0

you can use universalImageLoader library for downloading images in an efficient way.

see this how to use universal Image loader for downloading images

One more solution for your requirement is Volley Library

Community
  • 1
  • 1
Naveen
  • 1,948
  • 4
  • 17
  • 21