毕加索使用自定义的磁盘缓存 [英] Using Picasso with custom disk cache

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

问题描述

排球库中, 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.

我从排球切换到改造,以及用于图像,我决定尝试毕加索

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

使用前的库,我在每一个包含图片的网址,然后我用我的项目字符串参数 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.

我希望能够与毕加索做同样的,这可能与毕加索 API或我应该用自己的code这样的功能?

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(文件downloadedimage)上完成。这是正确的方式还是有什么最佳做法?

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?

推荐答案

毕加索没有一个磁盘缓存。它委托给任何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中(通过<一个href="https://developer.android.com/reference/java/net/ResponseCache.html"><$c$c>ResponseCache或OkHttp(通过 ResponseCache中或<一href="http://square.github.io/okhttp/javadoc/com/squareup/okhttp/OkResponseCache.html"><$c$c>OkResponseCache)存储在您需要的格式文件。我会强烈但是反对这项建议。

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!

您可以致电 setIndicatorsEnabled(真)毕加索实例,看看从那里图片加载一个指标。它看起来是这样的:

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.

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

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