3

When I use ListView the getView() method is called many times. Every time when the getView() is called i load the image with Asyc task. I mean every time i reset the image which is annoying.

How to understand when to load the image?

Bhavin Nattar
  • 3,189
  • 2
  • 22
  • 30
  • You need to lazy load your images. http://stackoverflow.com/questions/541966/how-do-i-do-a-lazy-load-of-images-in-listview – Knossos Jul 10 '13 at 11:24
  • Are You loading images from network? If so, then have You tried https://developers.google.com/events/io/sessions/325304728 which did loading/caching etc for You? – sandrstar Jul 10 '13 at 11:35

5 Answers5

0

You should cache loaded images, by storing i.e. on SD card, so once you got a copy there, no need to download it again. There's lot of ready-to-use classes that can do the job for you, like:

http://greendroid.cyrilmottier.com/reference/greendroid/widget/AsyncImageView.html

Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
0

you must must have two flags.

One which says if you've already loaded the image, if true you do nothing.

One which says if you're currently loading the image, if true you do nothing.

The members will also help you on maintaining the state of the image.

Your code should look something like this:

private boolean isLoading = false;
private boolean hasLoaded = false;

if(!hasLoaded){
       if(!isLoading){
          isLoading = true;
          //do async load
          //on positive completition callback set hasLoaded to true
          //on negative completition callback set isLoading to false
       }
}
Sam Aleksov
  • 1,201
  • 6
  • 12
0

One of the best solution is to create image cache using the WeakReference. This way you can keep images in memory and only need load from server when they are not in memory. In this method the image would be removed from the memory when system encounter low memory situation. So your current activity would always keep the hard reference to the bitmap's required and the image cache would keep the weak reference to the bitmap's.

below reference links will help you

http://developer.android.com/training/displaying-bitmaps/cache-bitmap.html

http://www.codeproject.com/Articles/35152/WeakReferences-as-a-Good-Caching-Mechanism

Nilanchala
  • 5,891
  • 8
  • 42
  • 72
  • Using weak references for caching on Dalvik is not a very good idea. The GC quite aggressively collects them and your cache won't be very effective. – laalto Jul 10 '13 at 13:59
0

the Volley library (made by google) has a very intuitive class for an imageView that can have a url , called "NetworkImageView" .

you should check it out and watch the video, since they show that it's quite annoying to do it using asyncTask (plus the asyncTask is known to have a limit of tasks, about 255 or so) .

for setting the url, just use setImageUrl .

it has some useful methods for the phases of loading too: setDefaultImageResId , setErrorImageResId.

it's also supposed to have built in caching mechanism of some sort, but i haven't read much about it, so you might want to check out their samples.

this will remove the need to use asyncTasks for the listView's items.

one of my questions regarding the volley includes a sample code , here .

Community
  • 1
  • 1
android developer
  • 114,585
  • 152
  • 739
  • 1,270
0

You can add a caching layer and optionally preloading the images. A good strategy for caching Images (Bitmap objects to be exact) is to use a strategy called LRU or least recently used.

Android support library has a class called LruCache that implements this strategy. So for example, when you download/load the image for the first time, you stick it into the cache. later, you can first check if it's already in cache and load it from there.

For preloading, A good rule of thumb is to preload the previous ten and the next ten items.

Nima
  • 6,383
  • 7
  • 46
  • 68