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

柔情痞子 提交于 2019-12-10 11:37:11

问题


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

回答1:


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


来源:https://stackoverflow.com/questions/39137700/how-to-display-image-from-cache-using-picasso-image-loader-when-wifi-is-off

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!