Question

I am using Volley for caching of images in the app. The memory caching is working fine, but no image is getting cached on the disk. The code is given below

VolleySingleton.java

public class VolleySingleton {

    public static final String TAG = "VolleySingleton";

     private static VolleySingleton mInstance = null;
    private RequestQueue mRequestQueue;
    private ImageLoader mImageLoader;

    private VolleySingleton(Context context){

        mRequestQueue = Volley.newRequestQueue(context);
        mImageLoader = new ImageLoader(this.mRequestQueue,new ImageLoader.ImageCache() {

            private final LruCache<String, Bitmap> mCache = new LruCache<String, Bitmap>(3);

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

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

    public static VolleySingleton getmInstance(Context context){
        if(mInstance == null){
            mInstance = new VolleySingleton(context);
        }
        return mInstance;
    }

    public RequestQueue getmRequestQueue(){
        return this.mRequestQueue;
    }

    public ImageLoader getmImageLoader(){
        return mImageLoader;
    }
}

The Images are loaded in a CustomAdapter ChannelAdapter.java

private class ChannelAdapter extends BaseAdapter{

    private LayoutInflater inflater;
    private VolleySingleton volleySingleton;

    public ChannelAdapter(Context context) {
        super(context);
        inflater = (LayoutInflater)             context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        volleySingleton = VolleySingleton.getmInstance(context);
    }


    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        if(view == null){
            view = inflater.inflate(R.layout.item,viewGroup,false);
        }
        NetworkImageView imageView = (NetworkImageView) view.findViewById(R.id.imageView);
        TextView textView = (TextView) view.findViewById(R.id.textView);
        textView.setText(tvChannel.getName());
        imageView.setImageUrl(tvChannel.getImages().get(link,volleySingleton.getmImageLoader());

        return  view;
    }
}
Was it helpful?

Solution

OK, i fixed the problem. It was because the images i obtained from the server had a cache-control tag set to no-cache on its response

Cache-Control: no-cache,no-cache,max-age=15552000

I made volley neglect this tag by modifiying HttpHeaderParser.java in the library. Now its working fine

OTHER TIPS

@ SathMk

I suggest you not to ignore any tag Cache-Control Header , if you really wish to Cache image longer when the header say no-cahce , then you can trick to increase the max-age if it is non-zero like in your cache. Other wise you wont get cache working properly for data request .

if (headerValue != null) {
        hasCacheControl = true;
        String[] tokens = headerValue.split(",");
        for (int i = 0; i < tokens.length; i++) {
            String token = tokens[i].trim();
            if (token.equals("no-cache") || token.equals("no-store")) {
                //Check if no-cache is there then still check again the max-age
                if (token.startsWith("max-age=")) {
                    try {
                        maxAge = Long.parseLong(token.substring(8));
                    } catch (Exception e) {
                        return null;
                    }
                }
            } else if (token.startsWith("max-age=")) {
                try {
                    maxAge = Long.parseLong(token.substring(8));
                } catch (Exception e) {
                }
            } else if (token.equals("must-revalidate") || token.equals("proxy-revalidate")) {
                maxAge = 0;
            }
        }
    }

Check if no-cache is there then still check again the max-age

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top