android - How do I use disk caching in Picasso? -


i using picasso display image in android app:

/** * load image.this within activity context activity */ public void loadimage (){     picasso picasso = picasso.with(this);      picasso.setdebugging(true);     picasso.load(quiz.getimageurl()).into(quizimage); } 

i have enable debugging , shows either red , green .but never shows yellow

now if load same image next time , internet not available image not loaded.

questions:

  1. does not have local disk cache?
  2. how enable disk caching using same image multiple times.
  3. do need add disk permission android manifest file?

this did. works well

first add okhttp gradle build file of app module

compile 'com.squareup.picasso:picasso:2.5.2' compile 'com.squareup.okhttp:okhttp:2.4.0' compile 'com.jakewharton.picasso:picasso2-okhttp3-downloader:1.0.2' 

then make class extending application

import android.app.application;  import com.squareup.picasso.okhttpdownloader; import com.squareup.picasso.picasso;  public class global extends application {     @override     public void oncreate() {         super.oncreate();          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);      } } 

add manifest file follows :

<application         android:name=".global"         .. >  </application> 

now use picasso would. no changes.

edit:

if want use cached images only. call library this. i've noticed if don't add networkpolicy, images won't show up in offline start even if cached. code below solves problem.

picasso.with(this)             .load(url)             .networkpolicy(networkpolicy.offline)             .into(imageview); 

edit #2

the problem above code if clear cache, picasso keep looking offline in cache , fail, following code example looks @ local cache, if not found offline, goes online , replenishes cache.

picasso.with(getactivity()) .load(imageurl) .networkpolicy(networkpolicy.offline) .into(imageview, new callback() {     @override     public void onsuccess() {      }      @override     public void onerror() {         //try again online if cache failed         picasso.with(getactivity())                 .load(posts.get(position).getimageurl())                 .error(r.drawable.header)                 .into(imageview, new callback() {             @override             public void onsuccess() {              }              @override             public void onerror() {                 log.v("picasso","could not fetch image");             }         });     } }); 

Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

thorough guide for profiling racket code -