0

I am using Picasso library for image download and displaying it in imageView. This library also store images in cache and memory. When my internet is turn on, i am able to view images on imageView. So i think, it should also be store in cache or file memory. Now my internet is turnOFF, but it doest not display to images. kindly have a look.

Picasso.with(context)
  .load(url) .placeholder(R.drawable.defaultimg)
  .networkPolicy(NetworkPolicy.OFFLINE)
  .into(holder.imageview2,  new ImageLoadedCallback(holder.loadingBar) {
    @Override
    public void onSuccess() {
        if (holder.loadingBar != null) {
            holder.loadingBar.setVisibility(View.GONE);
        }
    }
    @Override
    public void onError(){
        holder.loadingBar.setVisibility(View.VISIBLE);
        Picasso.with(context)
          .load(url) .placeholder(R.drawable.defaultimg)
          .into(holder.imageview2,  new ImageLoadedCallback(holder.loadingBar) {

            @Override
            public void onSuccess() {
                if (holder.loadingBar != null) {
                    holder.loadingBar.setVisibility(View.GONE);
                }
            }

            @Override
            public void onError() {
                if (holder.loadingBar != null) {
                    holder.loadingBar.setVisibility(View.GONE);
                }
            }
        });
    }
});
Paul
  • 4,160
  • 3
  • 30
  • 56
Rahul Rawat
  • 999
  • 2
  • 17
  • 40
  • When your internet is **OFF**, no matter images are cached or not, the source tends to be corrected for the library, so it throws exception and do not display the cached Image. Either download the image and provide the local path when internet is off, or provide a proper placeholder image in case of no internet. – Mohammed Atif Mar 20 '17 at 09:42
  • @MohammedAtif I don't believe that's the case. [Jake addressed this issue in the Picasso issue tracker here.](https://github.com/square/picasso/issues/698#issuecomment-58745858) – Paul Lammertsma Mar 20 '17 at 09:45
  • @Mohammed Atif my app that use picasso cache images, and display them even when internet is off. – Abdennacer Lachiheb Mar 20 '17 at 09:48
  • Please check http://stackoverflow.com/questions/22330772/why-use-android-picasso-library-to-download-images – Sangram Haladkar Mar 20 '17 at 09:49
  • In that case, did you try removing the NetworkPolicy builder? – Mohammed Atif Mar 20 '17 at 09:54
  • @MohammedAtif yes i added ** .networkPolicy(NetworkPolicy.OFFLINE)** so it will load from cache not internet but doest not work. – Rahul Rawat Mar 20 '17 at 09:57
  • Remove that and try. **Forces the request through the disk cache only, skipping network** Let it handle automatically. And on second note, I personally use Glide because of its efficiency and it handles the caching internally. – Mohammed Atif Mar 20 '17 at 09:59
  • @MohammedAtif Just removed. First all images are displaying. I closed to app , turnOFF internet, Open app Now no one images are displaying. – Rahul Rawat Mar 20 '17 at 10:07
  • Why don't you use `.error(R.drawable.defaultimg)` instead of calling `Picasso.with` for the second time? – dev.bmax Mar 20 '17 at 10:31
  • 1
    Check that the `url` is not empty / null when you're offline – dev.bmax Mar 20 '17 at 10:32
  • @dev.bmax Fantastic! Url was not correct. I was using ** http://xxxxx.com/fe9d6.jpg?960|720** this url but picasso autometically handle and rendering that url when internet ON. I just removed **960|720** and works – Rahul Rawat Mar 20 '17 at 10:43

1 Answers1

0

Finally I resolved that issue. Thanks @dev.bmax

image url was not correct. There is bug in Picasso. If we have url like as

https://i.ytimg.com/vi/DMVEcfQmPOs/maxresdefault.jpg?500|700

Picasso is able to displaying image when internet TURN ON but if we TURN OFF to internet, it does not decode the url. and not display the image also.

We have to remove ?500|700 then i was able to view image in OFFLine mode also. //url.substring(0,url.indexOf("?"))

https://i.ytimg.com/vi/DMVEcfQmPOs/maxresdefault.jpg

Thanks!

Rahul Rawat
  • 999
  • 2
  • 17
  • 40