0

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 ?

Bogdan Daniel
  • 2,689
  • 11
  • 43
  • 76
  • take a look here for an extensive tutorial about volley -- especially (7. Handling the volley Cache) -- http://www.androidhive.info/2014/05/android-working-with-volley-library-1/ – Tasos Nov 11 '15 at 19:11
  • @Tasos I tried to use the link you posted to come up with a solution but I can t seem to get it done. See my edit. – Bogdan Daniel Nov 11 '15 at 20:47
  • The disk cache if set will be available. If your server supports caching output, you don't need to set cache manually because volley does that for you. If not, IMHO, you can read my answer at the following http://stackoverflow.com/questions/31897189/android-setup-volley-to-use-from-cache/32022946#32022946 to see if it can help you or not. – BNK Nov 11 '15 at 21:35
  • @BNK I'm only using volley to load the image. For the server request I use async-http library. Should I use only volley instead for everything ? – Bogdan Daniel Nov 11 '15 at 21:38
  • Or should I keep using async-http(which I'm quite happy about) and switch to fresco instead of Volley ? – Bogdan Daniel Nov 11 '15 at 21:42
  • I haven't ever tried async-http, fresco :) – BNK Nov 11 '15 at 21:44
  • I dont think you understood (7. Handling the volley Cache). Volley has inbuilt cache, but as far as im aware you need to tell it, check in cache 1st and if you dont find the image download from URi. you can see it in action here http://www.androidhive.info/2014/07/android-custom-listview-with-image-and-text-using-volley/ under (AppController.java -- public ImageLoader getImageLoader() {) – Tasos Nov 13 '15 at 02:08
  • You also try Picasso. its very popular if you just handling images -- http://square.github.io/picasso/ – Tasos Nov 13 '15 at 02:16
  • Thank you, but I changed to Fresco – Bogdan Daniel Nov 13 '15 at 08:35

0 Answers0