0

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

pgSystemTester
  • 8,979
  • 2
  • 23
  • 49
urSus
  • 12,492
  • 12
  • 69
  • 89
  • I checked out a clean copy of Volley and it does the same thing. Its definatelly a bug... – urSus Jul 01 '13 at 21:19
  • No, i didnt check header from second image but maybe it has no-cache since it is genereted from svg – Selvin Jul 04 '13 at 21:15
  • How about this one? http://freesummer.sk/files/201106/thumb/big/images.jpg http://freesummer.sk/files/201106/thumb/big/fs2007_logo.jpg This is actually from my project – urSus Jul 04 '13 at 22:35
  • @Selvin I guess your right, the .svg and my project images have "Cache-Control:max-age=0" set on them, so they do not wish to be cached and Volley honours that? (Sorry I am new to this) – urSus Jul 05 '13 at 15:25
  • now i'm not so sure ... Google Chrome sends `max-age=0` too but it's getting 304 on second call from server so it takes images from cache even for this svg generated ... – Selvin Jul 05 '13 at 15:30
  • If it helps, it seems the getBitmap(cacheKey) is failing because BitmapFactory.decodeByteArray is returning null. (At least in my case). – ajacian81 Jul 23 '13 at 16:26

2 Answers2

1

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.

Itai Hanski
  • 8,540
  • 5
  • 45
  • 65
1

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);
    }
}