Search code examples
androidcachingpicassodisk

Picasso doesn't cache image on disk


I have to use custom OkHttpClient so I can add headers to the image requests. The problem is Picasso won't cache any images on disk because of this. I've used setIndicatorsEnabled(true) to check caching and I see only red indicators. When I use default OkHttpDownloader all is ok. Below is my Picasso initialization code. So does anyone encounter the same problem?

 public static void init(Context context) {
        Picasso.Builder builder = new Picasso.Builder(context);
        OkHttpClient client = new OkHttpClient();
        client.interceptors().add(new AuthInterceptor());
        Downloader downloader = new OkHttpDownloader(client);
        Picasso.setSingletonInstance(builder.downloader(downloader).build());
        Picasso.with(context).setIndicatorsEnabled(true);
    }

Also my image download code

 public static void load(final ImageView imageView, final Image image) {
            Picasso.with(imageView.getContext())
                    .load(image.getUrl())
                    .resize(400, 0)
                    .memoryPolicy(MemoryPolicy.NO_CACHE)
                    .into(imageView);
    }

Solution

  • Ah since this is happening when you change headers, you are most probably not setting the Cache-Control header

    According to Jake wharton (One of the developer of Picasso)

    Picasso doesn't have a disk cache. It delegates to whatever HTTP client you are using for that functionality (relying on HTTP cache semantics for cache control). Because of this, the behavior you seek comes for free

    Taken from Jake Wharton's answer here

    Also,

    If you never see a blue indicator, it's likely that your remote images do not include proper cache headers to enable caching to disk