Question

I've run into this weird error, where some images get cached as usual and some don't, any idea why? Both images do get displayed and memory cached just fine, but when offline some display error image.

For example, this works fine: http://cs4381.vk.me/u73742951/a_58a41ac2.jpg

However, this does not: http://upload.wikimedia.org/wikipedia/commons/thumb/d/d7/Android_robot.svg/220px-Android_robot.svg.png

Both work fine displaying and memcaching but the second doesn't get displayed from disk cache, although I think I see it being saved, as app says it has 12kB cache in the system settings

Edit

I checked out a clean copy of Volley and it does the same thing. Its definatelly a bug... From what Ive found out its that images do get cached, but Bitmap cachedBitmap = mCache.getBitmap(cacheKey); always returns null, so the cache says it doesnt have the bitmaps and then proceedes to download it again, and fail when offline, weird

Was it helpful?

Solution

The reason you're not getting any hits is because the default behavior in Volley for disk caching is dependent on the HTTP headers of the element you're requesting (in your case, an image).

Check the volley logs and see if you get the "cache-hit-expired" message - that means that the image was cached but it's TTL is expired as far as the default disk cache is concerned.

If you want the default settings to work, the images must have a Cache-Control header like max-age=??? where the question marks indicate enough seconds from the time it was downloaded.

If you want to change the default behavior, I'm not sure, but I think you have to edit the code a bit.

Look at the CacheDispatcher class in the Volley source.

Hope that helps.

OTHER TIPS

A quick and dirty way:

private static class NoExpireDiskBasedCache extends DiskBasedCache
{
    public NoExpireDiskBasedCache(File rootDirectory, int maxCacheSizeInBytes)
    {
        super(rootDirectory, maxCacheSizeInBytes);
    }
    public NoExpireDiskBasedCache(File rootDirectory)
    {
        super(rootDirectory);
    }
    @Override
    public synchronized void put(String key, Entry entry)
    {
        if (entry != null)
        {
            entry.etag = null;
            entry.softTtl = Long.MAX_VALUE;
            entry.ttl = Long.MAX_VALUE;
        }

        super.put(key, entry);
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top