Search code examples
androidpicassoimage-loadingoffline-mode

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


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

                    }
                });

Solution

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