Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Downloading images in background while showing the listview

I have an array of all the URIs of the images which I am showing in a List. Now I want to run a thread in background which gets this images from web and store them on the SD card. So when I click a particular element in the list instead of fetching from web it should fetch from the SD card in the new activity.

How do I do this?

like image 736
Ameya Phadke Avatar asked Jul 30 '10 18:07

Ameya Phadke


1 Answers

You could try using my lazy-drawables framework (GPL v3 licensed):

https://github.com/rtyley/lazy-drawables

-unfortunately it's not quite ready for production yet, but among the things it attempts to handle:

  • background (non-UI-thread) loading of image resources
  • efficient caching of scaled bitmaps - for the use-case where you're downloading a single big-ish image, and want to show it hundreds of times in a smaller icon format in a listview or something
  • transparency of use, by which I mean...

...once you have a lazy-drawable session set up, populating an image view is as easy as:

Drawable avatarDrawable = imageSession.get(someUserIdentifier); // doesn't block
imageView.setImageDrawable(avatarDrawable);

You don't care that it may have immediately returned you a normal bitmapdrawable, or possibly spawned an async task and returned a placeholder drawable - the drawable will update itself and it's host ImageView when the async task has completed.

That's the theory anyway. It seems to sorta-kinda work so far, but sometimes that spontaneous UI update doesn't happen... not sure why :-}

like image 98
Roberto Tyley Avatar answered Oct 17 '22 22:10

Roberto Tyley