我尝试使用picasso从网络服务器下载和缓存图片。我在这里找到了一个解决方案:https://stackoverflow.com/a/30686992/6884064
Picasso.Builder builder = new Picasso.Builder(this);
builder.downloader(new OkHttpDownloader(this,Integer.MAX_VALUE));
Picasso built = builder.build();
built.setIndicatorsEnabled(true);
built.setLoggingEnabled(true);
Picasso.setSingletonInstance(built);
Picasso.with(this)
.load("http://i.imgur.com/Q85lste.jpg")
.networkPolicy(NetworkPolicy.OFFLINE)
.into(coverImg);
的build.gradle:
compile 'com.squareup.picasso:picasso:2.5.2'
compile 'com.squareup.okhttp:okhttp:2.4.0'
compile 'com.squareup.okhttp:okhttp-urlconnection:2.2.0'
当我运行它时,占位符图像消失但新图像无法加载。
运行日志告诉我:
D/Picasso: Main created [R0] Request{http://i.imgur.com/Q85lste.jpg}
D/Picasso: Dispatcher enqueued [R0]+6ms
D/Picasso: Hunter executing [R0]+7ms
W/System.err: remove failed: ENOENT (No such file or directory) : /data/user/0/com.test.example1/cache/picasso-cache/journal.tmp
D/Picasso: Dispatcher batched [R0]+45ms for error
D/Picasso: Dispatcher delivered [R0]+246ms
D/Picasso: Main errored [R0]+246ms
任何人都知道这里发生了什么?谢谢!
修改
正在使用此代码:
OkHttpClient okHttpClient = new OkHttpClient();
okHttpClient.networkInterceptors().add(new Interceptor() {
@Override
public Response intercept(Interceptor.Chain chain) throws IOException {
Response originalResponse = chain.proceed(chain.request());
return originalResponse.newBuilder().header("Cache-Control", "max-age=" + (60 * 60 * 24 * 365)).build();
}
});
okHttpClient.setCache(new Cache(getCacheDir(), Integer.MAX_VALUE));
OkHttpDownloader okHttpDownloader = new OkHttpDownloader(okHttpClient);
Picasso picasso = new Picasso.Builder(this).downloader(okHttpDownloader).build();
picasso.load("http://i.imgur.com/test.jpg").into(coverImg);
答案 0 :(得分:1)
如果您尝试使用Android Studio模拟器,请清除缓存以及应用和卸载的数据,然后重新安装。 并且不要忘记所需的权限:
import java.io.*
也许有效。祝你好运。