【问题标题】:Why Picasso is not saving image in cache for offline use?为什么毕加索不将图像保存在缓存中以供离线使用?
【发布时间】:2017-10-16 06:40:39
【问题描述】:

我有以下代码,它不会按预期离线加载图像。它在线运行良好,但我也需要离线加载图像。我也允许写入外部存储。任何想法都会非常有帮助。

Picasso.with(getContext())
    .load(userInfo.getUserPictureUri())
    .networkPolicy(NetworkPolicy.OFFLINE)
    .resize(80, 80)
    .error(R.drawable.profile_picture)
    .centerCrop()
    .into(imageView_ProfilePictureSide, new Callback() {
        @Override
        public void onSuccess() {
        }

        @Override
        public void onError() {
            // Try again if cache failed
            Picasso.with(getActivity())
                 .load(userInfo.getUserPictureUri())
                 .error(R.drawable.profile_picture)
                 .into(imageView_ProfilePictureSide);
        }
    });

【问题讨论】:

  • 数据来自 GET 请求?
  • 哪些数据?是的,图片网址是获取请求
  • 你在你的 gradle 文件中添加了 compile 'com.squareup.okhttp:okhttp:2.4.0' 吗?
  • 感谢@KingofMasses 添加 okhttp 后它起作用了。如果可以的话,请将其作为答案发布,并说明毕加索需要 okhttp 的原因。我会接受的。对其他人也将非常有帮助。 :)

标签: android picasso


【解决方案1】:

将 OkHttp 添加到 app 模块的 gradle 构建文件中:

compile 'com.squareup.picasso:picasso:2.5.2'
compile 'com.squareup.okhttp:okhttp:2.4.0'

Picasso 使用HTTP 客户端请求到Disk Cache 操作因此您可以使您自己的http 请求标头具有属性Cache-Controlmax-age 并创建您自己的静态Picasso 实例而不是默认的Picasso 通过使用Okhttp .

Okhttppicasso 库均由 squareup 团队提供。

参考:How do I use disk caching in Picasso?Github issue about disk cache,@jake-wharton 已经回答了两个问题 -> Question1Question2

【讨论】:

    猜你喜欢
    • 2014-04-10
    • 1970-01-01
    • 2021-02-21
    • 2015-07-17
    • 2015-10-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-14
    相关资源
    最近更新 更多