3

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

                    }
                });
Jas
  • 3,207
  • 2
  • 15
  • 45
  • The image just won't show up on the screen. The entire program doesn't error out. – Tot Zam Nov 24 '15 at 04:39
  • So you are saying I don't have to write seperate code for offline right? – Jas Nov 24 '15 at 04:40
  • I only used this library once and my app seemed to work just fine offline, just no image of course. Simple way to make sure your app still works: turn the WiFi/internet on your device off before starting your app. – Tot Zam Nov 24 '15 at 04:44
  • If you want to show the images offline too then you will have to cache them. – George Thomas Nov 24 '15 at 04:51

2 Answers2

3

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
        }
    });
}
});
pRaNaY
  • 24,642
  • 24
  • 96
  • 146
1

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

w3bshark
  • 2,700
  • 1
  • 30
  • 40