casillas
casillas

Reputation: 16813

Volley Cache not working

I am trying to cache the downloaded images on the mainview and display them in the detail view. The way I am testing is as follows: first, I display all images on the gridview and disconnect the Internet and wanted to see cached image on the detail. However, on the detail activity the image is not being displayed.

I am using the following Volley Singleton class as follows:

public class CustomVolleyRequest {

    private static CustomVolleyRequest customVolleyRequest;
    private static Context context;
    private RequestQueue requestQueue;
    private ImageLoader imageLoader;

    private CustomVolleyRequest(Context context) {
        CustomVolleyRequest.context = context;
        this.requestQueue = getRequestQueue();

        imageLoader = new ImageLoader(requestQueue,
                new ImageLoader.ImageCache() {
                    private final LruCache<String, Bitmap>
                            cache = new LruCache<>(20);

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

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

    public static synchronized CustomVolleyRequest getInstance(Context context) {
        if (customVolleyRequest == null) {
            customVolleyRequest = new CustomVolleyRequest(context);
        }
        return customVolleyRequest;
    }

    public RequestQueue getRequestQueue() {
        if (requestQueue == null) {
            requestQueue = Volley.newRequestQueue(context.getApplicationContext(), 10 * 1024 * 1024);
        }
        return requestQueue;
    }

    ImageLoader getImageLoader() {
        return imageLoader;
    }
}

Main Activity Adapter

public View getView(final int position, View convertView, @NonNull ViewGroup parent) {
    final ViewHolder holder;
    if(convertView == null) {
        holder = new ViewHolder();
        convertView = LayoutInflater.from(getContext()).inflate(R.layout.grid_item, parent, false);
    }
  holder.imageView.setImageUrl(imageURL, mImageLoader);
}

Detail Activity

// I have checked and confirmed that the following url is same with the one the main activity
String url = getArguments().getString("image_url");
ImageLoader imageLoader = CustomVolleyRequest.getInstance(getContext()).getImageLoader();
mImageView.setImageUrl(url, imageLoader);

Upvotes: 1

Views: 595

Answers (1)

BNK
BNK

Reputation: 24114

I think Volley caching still works, however, your issue here is because of the NetworkImageView inside the detail activity, which is different from the NetworkImageView inside the main activity, it also needs to load the image successfully one time first. Here, you disconnect Internet before starting to the detail activity.

You can see the logic inside NetworkImageView.java

Pay attention to the lines from #122

// if there was an old request in this view, check if it needs to be canceled.
        if (mImageContainer != null && mImageContainer.getRequestUrl() != null) {...

and #134

// The pre-existing content of this view didn't match the current URL. Load the new image
        // from the network.
        ImageContainer newContainer = mImageLoader.get(mUrl,...

UPDATE 2017/02/10

When the NetworkImageView inside detail activity loads the new image from the network (ImageContainer newContainer = mImageLoader.get...), the method get inside ImageLoader.java will be called.

See the code:

...
final String cacheKey = getCacheKey(requestUrl, maxWidth, maxHeight);
// Try to look up the request in the cache of remote images.
Bitmap cachedBitmap = mCache.getBitmap(cacheKey);
if (cachedBitmap != null) {
    // Return the cached bitmap.
    ImageContainer container = new ImageContainer(cachedBitmap, requestUrl, null, null);
    imageListener.onResponse(container, true);
    return container;
}
...

You will find that if the the image views in both activities have the same width and height, then cacheKey will be the same too and the cachedBitmap will be not null, cached bitmap will be used for the NetworkImageView inside detail activity also.

Upvotes: 2

Related Questions