【问题标题】:Picasso memory/disk cachePicasso 内存/磁盘缓存
【发布时间】:2015-10-01 19:47:57
【问题描述】:

我使用 Picasso 从 Web 下载图像,并将它们显示在 RecyclerView 中。

private Picasso createPicasso(Context context){

        OkHttpClient picassoClient = new OkHttpClient();

        picassoClient.interceptors().add(new Interceptor() {
            @Override
            public Response intercept(Interceptor.Chain chain) throws IOException {

                try {

                    Map authHeaders = BackendServiceClient.buildAuthHeaders();

                    Request newRequest = chain.request().newBuilder()
                            .addHeader("Authorization", (String) authHeaders.get("Authorization"))
                            .build();
                    return chain.proceed(newRequest);

                } catch (CredentialNotStoredException e) {
                    e.printStackTrace();
                }
                return chain.proceed(chain.request().newBuilder().build());
            }
        });

        return new Picasso.Builder(context)
                .downloader(new OkHttpDownloader(picassoClient))
                .build();
    }

用法:

  ImageDownloader.getSharedInstance().getPicasso(context)
            .load(url)
            .placeholder(R.drawable.head_big) //
            .error(R.drawable.head_big) //
            .tag(context)
            .into(holder.personPhoto);

下载和显示图像按预期工作,但如果我滚动列表,图像会再次从网络获取并且没有被缓存。 如何始终将它们缓存在磁盘和内存中。

【问题讨论】:

  • ....服务器端必须支持缓存...
  • 没有办法强制将图片缓存在磁盘上?
  • 第一个使用正确关键字的谷歌搜索返回了这个库的创建者的答案......永远不要低估谷歌搜索的力量

标签: android caching picasso


【解决方案1】:

切换到 Glide,它可以改善内存占用和开箱即用的缓存。

https://github.com/bumptech/glide

http://inthecheesefactory.com/blog/get-to-know-glide-recommended-by-google/en

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-07-21
    • 2018-08-22
    • 2015-11-15
    • 2016-06-12
    • 2017-11-19
    • 2016-03-16
    • 2013-09-07
    相关资源
    最近更新 更多