3

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);
    }
HellCat2405
  • 752
  • 7
  • 16
  • why are you doing this `.memoryPolicy(MemoryPolicy.NO_CACHE)` if you want memory cache – Bhargav Mar 04 '16 at 21:46
  • I actually don't want the memory cache, I want only disk cache. – HellCat2405 Mar 04 '16 at 21:47
  • Ah since this is happening when you change headers, you are most probably not setting the `Cache-Control` header – Bhargav Mar 04 '16 at 21:54
  • strangely adding 'header("Cache-Control", "max-age=600")' won't help, Picasso is still using only network – HellCat2405 Mar 04 '16 at 22:05
  • 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 http://stackoverflow.com/a/23281195/4128945 – Bhargav Mar 04 '16 at 22:09
  • 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, his quote – Bhargav Mar 04 '16 at 22:10
  • 1
    thanks, so I guess I have to switch to Glide – HellCat2405 Mar 04 '16 at 22:16
  • ill post this as answer with the link to Jake's answer – Bhargav Mar 04 '16 at 22:21
  • Btw why can't you set the headers yourself? – Bhargav Mar 04 '16 at 22:23
  • I did set them but I guess the response images do not include proper cache headers to enable caching – HellCat2405 Mar 06 '16 at 16:33

1 Answers1

1

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

Community
  • 1
  • 1
Bhargav
  • 8,118
  • 6
  • 40
  • 63