为什么 Picasso 不将图像保存在缓存中以供离线使用?

Why Picasso is not saving image in cache for offline use?

我有以下代码,它没有按预期离线加载图像。它在线运行良好,但我也需要离线加载图像。我也已授予写入外部存储的权限。任何想法都会很有帮助。

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

将 OkHttp 添加到应用程序模块的 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? and Github issue about disk cache, two Questions has been answered by @jake-wharton -> Question1 and Question2