hadez30
hadez30

Reputation: 503

Android Volley ListView Images Not Loading When Using DiskBasedCache

I have an app that loads images on each item on a list view, and I use Volley to make life easier for me; I need to have to images loaded from disk if it's already been downloaded before.

Problem: It won't work. It needs to re-download the images all over again. I need to have the image saved even after I exit the app.

Weird: It works only on one particular image (and it has nothing to do with size)!

What I Used: I patterned this using this site: https://github.com/rdrobinson3/VolleyImageCacheExample.

I also tried this: http://howrobotswork.wordpress.com/2013/06/02/downloading-a-bitmap-asynchronously-with-volley-example/

The Code:

String godzilla = "http://vineland.pynchonwiki.com/wiki/images/c/cf/Godzilla.jpg";

//String meme = "http://upload.wikimedia.org/wikipedia/en/d/d7/Meme_Many_Journeys.jpg";
ImageCacheManager.getInstance().getImageLoader().get(godzilla, new ImageLoader.ImageListener() {
    @Override
    public void onResponse(ImageLoader.ImageContainer imageContainer, boolean b) {
           viewHolder.backgroundImage = imageContainer.getBitmap();
           updateBackgroundImage(viewHolder,  viewHolder.backgroundImage, object);
           updateLayoutAlignmentParams(viewHolder);
    }

    @Override
    public void onErrorResponse(VolleyError volleyError) {

    }
});
if(viewHolder.backgroundImage != null)
    updateBackgroundImage(viewHolder, viewHolder.backgroundImage, object);

I've tried the meme website, and it still has problems. I had one particular site that contains an image that oddly works. Which makes it even more confusing.

Edit: Additional info, it seems like there's an error on adding lruEntries as lruEntries.remove(entry.key is being called on completeEdit().

Upvotes: 2

Views: 1932

Answers (1)

Itai Hanski
Itai Hanski

Reputation: 8690

Volley has 2 cache layers when it comes to images:

  1. The L1 level: a memory cache provided by you in the ImageLoader constructor.
  2. The L2 level: a disk cache that is shared among every request performed by the same RequestQueue.

The disk cache caches every response unless explicitly requested not to by the request. But, the caching is performed according to the HTTP cache headers of the response.

When you request an image, this is what Volley does:

  1. Check L1 cache for image. Return if found.
  2. Image not in memory - Check L2 cache. If found check expiration of the cache headers. If still valid, add to L1 cache and return image.
  3. Image not on disk (either not there or expired) - Perform a network request. Cache the response in the disk cache and the bitmap in the memory cache and return.

I bet that the image that loaded from the disk has cache headers.

IMO, you have 3 options:

  1. The image server is yours - add the appropriate cache headers.
  2. The image server isn't yours - accept the fact the some images will not be cached on the disk.
  3. Override the caching policy to better suit your needs. This means editing the Volley source code.

Upvotes: 3

Related Questions