Reputation: 119
I'm trying to download and cache an image with picasso from a webserver. I found a solution right here: 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'
When I run it my placeholder image disappears but the new image doesn't load in.
The run log gives me this:
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
Anyone know what's going on here? Thanks!
EDIT:
It's working with this code:
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);
Upvotes: 2
Views: 1556
Reputation: 1135
If you try in android Studio emulator, clean Cache and data of app and uninstall and then install it again. And also don't forget required permissions:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
maybe worked. good luck.
Upvotes: 1