user2391218
user2391218

Reputation: 412

How is image caching implemented in android - volley?

From what I understand we need to provide the implementation of LRU cache and pass the same as constructor to the image loader.

There is also a default disk based cache present in volley. This disk based cache is used for caching HTTP responses.

**Which cache will be used when the image downloaded contains cache headers ??

  1. LRU(own implementation)
  2. Default disc cache implementation present inside volley toolbox package*strong text* ** to public class BitmapLruCache extends LruCache implements ImageLoader.ImageCache

{

public static int getDefaultLruCacheSize() {
    final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);
    final int cacheSize = maxMemory / 8;

    return cacheSize;
}

public BitmapLruCache() {
    this(getDefaultLruCacheSize());
}

public BitmapLruCache(int sizeInKiloBytes) {
    super(sizeInKiloBytes);
}

@Override
protected int sizeOf(String key, Bitmap value) {
    return value.getRowBytes() * value.getHeight() / 1024;
}

@Override
public Bitmap getBitmap(String url) {
    return get(url);
}

@Override
public void putBitmap(String url, Bitmap bitmap) {
    put(url, bitmap);
}}

This is the call which is used to set the Image in the network view

ImageLoader mImageLoader = new          ImageLoader(ApplicationController.getInstance().getRequestQueue(), new BitmapLruCache());
 holder.imageicon.setImageUrl(i.getThumb_image(), mImageLoader);

Upvotes: 0

Views: 1309

Answers (1)

Submersed
Submersed

Reputation: 8870

The LRU cache is used for caching the images. See the source code for ImageLoader - uses an ImageCache, which is an interface volley provides you to implement in your extended LruCache. So, your cache would both extend LruCache<String,Bitmap> and implement an ImageCache.

The default cache that volley provides is used to every response it gets, with the default strat of caching according to cache headers of the HTTP response. So, if they have the right cache headers, they'll be cached on disk, if they don't, they won't.

So, to answer your question in full, potentially both caches will be used -- but as far as retrieving the images go, the in memory cache (or your lru cache) will be used.

Upvotes: 0

Related Questions