Picasso Library, Android: Using Error Listener

Paul Ruiz picture Paul Ruiz · Jun 11, 2013 · Viewed 31k times · Source

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

Answer

dnkoutso picture dnkoutso · Jul 30, 2013

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.