毕加索不在磁盘上缓存图像

Picasso doesn't cache image on disk

我必须使用自定义 OkHttpClient,这样我才能将 headers 添加到图像请求中。问题是 Picasso 不会因此而在磁盘上缓存任何图像。我使用 setIndicatorsEnabled(true) 检查缓存,但只看到红色指示器。当我使用默认的 OkHttpDownloader 时,一切正常。下面是我的 Picasso 初始化代码。那么有没有人遇到同样的问题呢?

 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);
    }

还有我的图片下载码

 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);
    }

啊,因为当您更改 headers 时会发生这种情况,您很可能没有设置 Cache-Control header

根据 Jake wharton(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

摘自 Jake Wharton 的回答 here

此外,

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