毕加索不在磁盘上缓存图像 [英] Picasso doesn't cache image on disk

查看:91
本文介绍了毕加索不在磁盘上缓存图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须使用自定义OkHttpClient,以便可以将标头添加到图像请求.问题在于,因此,毕加索不会在磁盘上缓存任何图像.我已经使用setIndicatorsEnabled(true)来检查缓存,并且我只看到红色指示器.当我使用默认的OkHttpDownloader时,一切正常.以下是我的毕加索初始化代码.那么有人会遇到同样的问题吗?

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

还有我的图片下载代码

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

推荐答案

由于更改标头时会发生这种情况,因此您很可能没有设置Cache-Control标头

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没有磁盘缓存.它委托给任何HTTP 用于该功能的客户端(依赖于HTTP缓存) 缓存控制的语义).因此,您寻求的行为 免费提供

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的回答此处

Taken from Jake Wharton's answer 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

这篇关于毕加索不在磁盘上缓存图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆