Search code examples
androidpicasso

How to Display Image From cache using picasso image loader when wifi is off?


Display Image from url using picasso image loader,it working fine but i wont display image from cache when wifi is off state.

 ImageView photo=(ImageView)findViewByid(R.id.photo);
 Picasso.with(context)
                    .load("thumburl")
                    .placeholder(R.mipmap.ic_launcher)
                    .error(R.mipmap.pattern1)
                    .into(photo);

Solution

  • first add the OkHttp to the gradle build file of the app module :

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

    then add this code to your class :

      Picasso.with(context).load("thumburl")
                            .networkPolicy(NetworkPolicy.OFFLINE) 
                            .into(photo, new Callback() {
                                @Override 
                                public void onSuccess() { 
    
                                } 
    
                                @Override 
                                public void onError() { 
                                  //Try for online if no data in cached or cached failed 
                                    Picasso.with(context) 
                                            .load("thumburl") 
                                    .placeholder(R.mipmap.ic_launcher)
                                    .error(R.drawable.user_error)
                                            .into(photo); 
                                } 
                            });