javaandroidcachingif-modified-sinceetag

Android Image loader library with support for ETags, If-Modified-Since


I've been using for some time Android Http Image Manager, and recently I switched over to Android Universal Image Loader

I'm afraid neither of them have support for verifying if local cache is up-to-date.

What I'm currently looking for is an Image Loader library with community support and support for checking remote changes via ETag and/or If-Modified-Since


Solution

  • Question is answered from Github AUIL issue tracker. Thank you NOSTRA

    https://github.com/nostra13/Android-Universal-Image-Loader/issues/75

    public class URLConnectionImageDownloader extends ImageDownloader {
        @Override
        public InputStream getStreamFromNetwork(URI imageUri) throws IOException {
            URLConnection conn = imageUri.toURL().openConnection();
            // check etag/last-modification-date/... params
            // if image was changed then we should delete cached image from memory cache and disc cache
    
            // Delete image from caches
            String uri = imageUri.toString();
            File imageFile = ImageLoader.getDiscCache().get(uri)
            if (imageFile.exists()) {
                imageFile.delete();
            }
            MemoryCacheAware<String, Bitmap memoryCache = ImageLoader.getMemoryCache();
            for (String cacheKey : memoryCache.keys()) {
                if (cacheKey.contains(uri) {
                    memoryCache.remove(cacheKey);
                }
            }
    
    
            return new FlushedInputStream(new BufferedInputStream(conn.getInputStream()));
        }
    }