Search code examples
androidandroid-volleyandroid-lru-cache

Accessing cached Images from LruCache Implementation


I have a GridView with images and i have populated it using Volley and cached the images using the below links. I want to access the selected image of GridView in a new activity from the cached implementation. I have searched here on StackOverflow but could not find how to access the cached images using their key(int this case the URL). i have followed these links but couldn't find anything:

Jake Whartons Volley CUstomization

VolleyImageCacheExample

Do I have to use the same LruCache object created in ImageLoder object in the second activity or is there any other way?

Imageloader object in Singleton class

This is the default implementation of LruCache object used. If someone could explain using the code below?

imageLoader = new ImageLoader(requestQueue, new ImageLoader.ImageCache() {

        private final LruCache<String, Bitmap> cache = new LruCache<>(10);

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

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

Assuming that i have a custom class BitmapCache that extends LruCache and implements ImageCache, please guide me how can i access the cache in 2nd activity to access the images cached

public class LruCacheClass extends LruCache<String,Bitmap> implements ImageLoader.ImageCache {

public LruCacheClass(int maxSize) {
    super(maxSize);
}



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



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

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

    put(url, bitmap);

}}

Solution

  • I have found the answer here

    The reason was that i had resized the ImageView while getting the image from cache. Cache only returns the image that has the size that of the originally saved.