Picasso doesn't cache image on disk

非 Y 不嫁゛ 提交于 2019-12-07 12:37:14

问题


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

回答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



来源:https://stackoverflow.com/questions/35806571/picasso-doesnt-cache-image-on-disk

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!