25

I'm playing around with the Picasso library for image loading, but I'm running into an issue. When an image fails to load, I want to hide the view rather than load in a default image. I noticed from the source that it looks like the only way to add a listener is from the builder, but the error method is never called when an image does fail to load. Anyone have any experience with this?

    iv = (ImageView) findViewById(R.id.imageView);

    Picasso.Builder builder = new Picasso.Builder(getApplicationContext());
    builder.listener(new Picasso.Listener() {

        @Override
        public void onImageLoadFailed(Picasso arg0, String arg1) {
            Log.e("Picasso Error", "Errored out, hiding view");
            iv.setVisibility(View.GONE);
        }
    });
    Picasso pic = builder.build();
    pic.load("thisshouldbreak.jpg").into(iv);
Paul Ruiz
  • 2,396
  • 3
  • 27
  • 47

6 Answers6

52

Picasso 2.0 allows you to attach a callback into a request.

https://github.com/square/picasso

The callback you are using is for "global" listener and it helps you debug errors that potentially happen due to a network load.

Use load(url).into(view, new Callback() {...}); in Picasso 2.0.

Remember to invoke cancelRequest(target) if you are using a Callback.

dnkoutso
  • 6,041
  • 4
  • 37
  • 58
19

My example:

Picasso picasso = new Picasso.Builder(parent.getContext())
            .listener(new Picasso.Listener() {
                @Override
                public void onImageLoadFailed(Picasso picasso, Uri uri, Exception exception) {
                //Here your log
                }
            })
            .build();
    picasso.load(shopModel.getShopImg())
            .fit()
            .into(viewHolder.shopImg);
Glorfindel
  • 21,988
  • 13
  • 81
  • 109
5

You can try to add a 'global' listener.

    // create Picasso.Builder object
    Picasso.Builder picassoBuilder = new Picasso.Builder(this);

    picassoBuilder.listener(new Picasso.Listener() {
        @Override
        public void onImageLoadFailed(Picasso picasso, Uri uri, Exception exception) {
            Log.e("PICASSO", uri.toString(), exception);
        }
    });

    // Picasso.Builder creates the Picasso object to do the actual requests
    Picasso picasso = picassoBuilder.build();

    try {
        Picasso.setSingletonInstance(picasso);
    } catch (IllegalStateException ignored) {
        // Picasso instance was already set
        // cannot set it after Picasso.with(Context) was already in use
    }

Any subsequent calls to Picasso.with(Context context) will return the instance which connected to listener, so all fails will be logged.

Please note that you need to call setSingletonInstance as soon as possible, e.g. in Application onCreate.

P.S. Code adopted from here - Customizing Picasso with Picasso.Builder

Anton Malmygin
  • 3,406
  • 2
  • 25
  • 31
4

My answer:

File file = new File(filePath);
        Picasso.with(context).load(file).placeholder(R.drawable.draw_detailed_view_display).error(R.drawable.draw_detailed_view_display)
        .resize(400, 400).into(mImageView, new Callback() {

            @Override
            public void onSuccess() {

            }

            @Override
            public void onError() {
                mImageView.setVisibility(View.GONE);
            }
        });
TVT. Jake
  • 279
  • 2
  • 7
2

When we got error, error goes to onError method then we handle it!

private void getAvatar(){
        Picasso.with(this)
                .load(Links.GET_AVATAR + ".jpg")
                .into(imgUserAvatar, new Callback() {
                    @Override
                    public void onSuccess() {

                    }

                    @Override
                    public void onError() {
                        imgUserAvatar.setImageResource(R.drawable.icon_profile_default);
                    }
                });
}
Hadi Note
  • 1,386
  • 17
  • 16
1

Just a suggestion, but you might avoid issues in programming if you make an "empty" png file and set it as the default image file in your res folder... kinda silly i know... but likely to work without fighting...

me_
  • 681
  • 1
  • 8
  • 18
  • not a practical way. of course in some cases if image url is not a valid format its a good suggestion. but what if some url are 1. //cdn.bla2.com/sample.png 2. http://cdn.bla2.com/sample .png in this case it errors because of only the prefix so its good to check this error as well – Emil Reña Enriquez Jan 29 '15 at 04:50
  • it works, but thanks for taking to time to vote it down emil... the point of offering a solution is that the solution works – me_ Jan 29 '15 at 06:37
  • 2
    by the way emil... you might want to check out the dates... i solved his problem before picasso 2 came out... of course the younger answer depends on it... – me_ Feb 04 '15 at 09:09