Does Picasso library for Android handle image loading while network connectivity is off?

末鹿安然 提交于 2019-12-21 05:47:13

问题


I'm working on an application in which i use Picasso library for image loading in my ViewPager and other ImageViews. So i want to know what happens if network connectivity is off. Does the library handle itself or do i have to check the network connectivity before loading image to views?

My code:

Picasso picasso = Picasso.with(getActivity());
        picasso.setDebugging(true);
        picasso.load(downloadPath+imgDetailPhoto)
                .placeholder(R.drawable.no_image)
                .error(android.R.drawable.stat_notify_error)
                .into(eventImage, new Callback() {
                    @Override
                    public void onSuccess() {
                         Log.d("Success...", "picasso loaded successfully");
                    }

                    @Override
                    public void onError() {
                        Log.d("Error...", "picasso load error");

                    }
                });

回答1:


Using below code Picasso caches images for offline use.

Picasso.with(this)
        .load(downloadPath+imgDetailPhoto)
        .placeholder(R.drawable.no_image)
        .error(android.R.drawable.stat_notify_error)
        .networkPolicy(NetworkPolicy.OFFLINE)//use this for offline support
        .into(eventImage);

Above code is not worke while removing cache.so Picasso can't find image from cache.If not get image from cache we handle to get image online and display it. We achieve that using below code:

Picasso.with(getActivity())
.load(downloadPath+imgDetailPhoto)
.placeholder(R.drawable.no_image)
.error(android.R.drawable.stat_notify_error)
.networkPolicy(NetworkPolicy.OFFLINE)//user this for offline support
.into(eventImage, new Callback() {
@Override
public void onSuccess() {

}

@Override
public void onError() {
          Picasso.with(getActivity())
.load(downloadPath+imgDetailPhoto)
.placeholder(R.drawable.no_image)
.error(android.R.drawable.stat_notify_error)
.networkPolicy(NetworkPolicy.OFFLINE)//user this for offline support
.into(eventImage, new Callback() {
        @Override
        public void onSuccess() {

        }

        @Override
        public void onError() {
           //get error if image not loaded
        }
    });
}
});



回答2:


Picasso caches images for offline use. I'm using it in a simple movie app where I display a bunch of movie posters. I can turn on airplane mode and my images are still there. Likewise, if I force close the application while in airplane mode, then open the app again, my images will still load.

Hope this helps.

P.S. check out Glide https://github.com/bumptech/glide. It's faster and has smoother loading than Picasso



来源:https://stackoverflow.com/questions/33885561/does-picasso-library-for-android-handle-image-loading-while-network-connectivity

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