This is my CustomVolleyRequestQueue
private CustomVolleyRequestQueue(Context context) {
mCtx = context;
mRequestQueue = getRequestQueue();
mImageLoader = new ImageLoader(mRequestQueue,
new ImageLoader.ImageCache() {
private final LruCache<String, Bitmap>
cache = new LruCache<String, Bitmap>(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 CustomVolleyRequestQueue getInstance(Context context) {
if (mInstance == null) {
mInstance = new CustomVolleyRequestQueue(context);
}
return mInstance;
}
public RequestQueue getRequestQueue() {
if (mRequestQueue == null) {
Cache cache = new DiskBasedCache(mCtx.getCacheDir(), 10 * 1024 * 1024);
Network network = new BasicNetwork(new HurlStack());
mRequestQueue = new RequestQueue(cache, network);
// Don't forget to start the volley request queue
mRequestQueue.start();
}
return mRequestQueue;
}
public ImageLoader getImageLoader() {
return mImageLoader;
}
}
I use it to display NetworkImages in a RecyclerAdapter. This is how I initialize the imageLoader ImageLoader mImageLoader = CustomVolleyRequestQueue.getInstance(this.getActivity())
.getImageLoader();
and this is how I use it :
holder.imageView.setImageUrl(listItems.getImagePath(), mImageLoader);
getImagePath()
- simply returns an url
Everything is working fine. What I want to do now is to retrieve that cache and display the images from cache if it is present(no internet) after the app was killed. How can I achieve this ?
Edit : I added this to be done when I click a button.
Cache cache = CustomVolleyRequestQueue.getInstance(getActivity()).getRequestQueue().getCache();
Cache.Entry entry = cache.get("urlThatIKnowWasLoaded");
if (entry != null) {
try {
Log.d("askj","Cache is present");
String data = new String(entry.data, "UTF-8");
// handle data, like converting it to xml, json, bitmap etc.,
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
} else
Log.d("askj","Cache is not present");
{
// Cached response doesn't exists. Make network call here
}
I click this button after the data was loaded so I know that it is present in the cache(I can go back form that Activity and reenter in it and the images are there in no time), but I get that "Cache is not present". Clearly this approach has something wrong. How can I solve it ?