将 Picasso 与自定义磁盘缓存一起使用 [英] Using Picasso with custom disk cache

查看:33
本文介绍了将 Picasso 与自定义磁盘缓存一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Volley 库中,NetworkImageView 类需要一个 ImageLoader,它通过在 ImageCache 中搜索来处理所有图像请求 实现,用户可以自由选择缓存的工作方式、图像的位置和名称.

In Volley library, the NetworkImageView class requires an ImageLoader that handles all the image requests by searching for them inside an ImageCache implementation, the user is free to choose how the cache should work, the location and the name of the images.

我正在从 Volley 切换到 Retrofit,对于图像,我决定尝试 Picasso.

I'm switching from Volley to Retrofit, and for the images I decided to try Picasso.

对于以前的库,我在每个包含图像 URL 的项目中都有一个 String 参数,然后我使用了 myNetworkImageView.setImageUrl(item.getURL()) 并且它能够确定是否图像缓存在磁盘上.如果图片存在于缓存文件夹中,则加载图片,否则下载并加载.

With the former library, I had a String parameter in each of my items containing the image URL, then I used myNetworkImageView.setImageUrl(item.getURL()) and it was able to determine if image was cached on disk. If the image existed in cache folder, the image was loaded, otherwise it was downloaded and loaded.

我希望能够用 Picasso 做同样的事情,是否可以用 Picasso API 或者我应该自己编写这样的功能?

I would like to be able to do the same with Picasso, is it possible with Picasso APIs or should I code such feature by myself?

我想将图像下载到一个文件夹(缓存文件夹),并在完成时使用 Picasso.with(mContext).load(Filedownloadedimage).这是正确的方法还是有任何最佳实践?

I was thinking to download the image to a folder (the cache folder), and use Picasso.with(mContext).load(File downloadedimage) on completion. Is this the proper way or are there any best practices?

推荐答案

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.

只有在本地缓存中不存在图像(并且该图像未过期)时,底层 HTTP 客户端才会通过网络下载图像.

The underlying HTTP client will only download an image over the network if one does not exist in its local cache (and that image isn't expired).

也就是说,您可以java.net.HttpUrlConnection创建自定义缓存实现(通过ResponseCache 或 OkHttp(通过 ResponseCacheOkResponseCache) 以您想要的格式存储文件.但是,我强烈建议不要这样做.

That said, you can create custom cache implementation for java.net.HttpUrlConnection (via ResponseCache or OkHttp (via ResponseCache or OkResponseCache) which stores files in the format you desire. I would strongly advise against this, however.

让毕加索和 HTTP 客户端为您完成工作!

Let Picasso and the HTTP client do the work for you!

您可以在 Picasso 实例上调用 setIndicatorsEnabled(true) 以查看加载图像的指示器.它看起来像这样:

You can call setIndicatorsEnabled(true) on the Picasso instance to see an indicator from where images are being loaded. It looks like this:

如果您从未看到蓝色指示器,则可能是您的远程图像不包含正确的缓存标头来启用缓存到磁盘.

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.

这篇关于将 Picasso 与自定义磁盘缓存一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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