Android: Picasso load image failed . how to show error message
Asked Answered
P

7

58

I am trying to use the picasso library to loading the image store in the mediastore. When I called load(imageview, callback), the picasso call onFail instead of onSuccess. How do I know why the image was not loaded successfully?

Pallaten answered 9/9, 2014 at 12:14 Comment(1)
Have a look at this answered question [Picasso Library, Android: Using Error Listener][1] [1]: #17053038Merce
D
188

Use builder:

    Picasso.Builder builder = new Picasso.Builder(this);
    builder.listener(new Picasso.Listener()
    {
        @Override
        public void onImageLoadFailed(Picasso picasso, Uri uri, Exception exception)
        {
            exception.printStackTrace();
        }
    });
    builder.build().load(URL).into(imageView);

Edit

For version 2.71828 they have added the exception to the onError callback:

        Picasso.get()
            .load("yoururlhere")
            .into(imageView, new Callback() {
                @Override
                public void onSuccess() {
                }

                @Override
                public void onError(Exception e) {
                }
            })
Domingadomingo answered 3/6, 2015 at 19:0 Comment(0)
G
15

When you use callback, the picaso will call method onSuccess and onError!

File fileImage = new File(mPathImage);
        Picasso.with(mContext).load(fileImage)
                .placeholder(R.drawable.draw_detailed_view_display)
                .error(R.drawable.draw_detailed_view_display)
                .resize(200, 200)
                .into(holder.mImageEvidence, new Callback() {
                    @Override
                    public void onSuccess() {
                        holder.mMediaEvidencePb.setVisibility(View.GONE);
                    }

                    @Override
                    public void onError() {
                        holder.mErrorImage.setVisibility(View.VISIBLE);
                    }
                });
Gonzalogoo answered 30/10, 2015 at 3:52 Comment(3)
how to get the cause of unable to load image in onError ?Hieratic
Method onError not offers information about the error. It only help you realize there is an error is occurring by show "mErrorImage"Gonzalogoo
That's what I know too. Global listener should be used instead.Hieratic
H
11

In case you want to use Picasso with Kotlin and lambda expression it could be as short as this:

val picasso = Picasso.Builder(context)
            .listener { _, _, e -> e.printStackTrace() }
            .build()

...and then you can load image as usual:

picasso.load(url).into(imageView)
Her answered 26/11, 2017 at 13:13 Comment(0)
S
3

Have you added internet permission in Manifest? With Kevin's answer here, Please see the exception log and post the exception here.

Selfsupport answered 30/8, 2017 at 4:51 Comment(0)
S
0
val picasso = Picasso.Builder(context).listener(
                    object : Picasso.Listener{
                        override fun onImageLoadFailed(picasso: Picasso?, uri: Uri?, exception: Exception?) {
                            exception?.printStackTrace()
                            println("Picasso loading failed : ${exception?.message}")
                        }

                    }
            ).build()
picasso.load(imageUrl).into(imageView)
Stephaniastephanie answered 28/9, 2020 at 8:0 Comment(0)
M
0

Please try this to check logs of the Picasso

Picasso.with(getContext()).setLoggingEnabled(true);
Merciless answered 25/11, 2020 at 12:5 Comment(0)
P
0
You have use picasso exception handler. Because if you are to use traditional exception handler(try / catch) it wont catch the actual exception.

var imgURL = Your image url
var imgHolder = Id of your image view

val picasso = Picasso.Builder(this@yourActivity)
            .listener { picasso, uri, exception ->
                //Here your log - Log cat - error
                Log.e("Exception ", exception.stackTraceToString())
            }
            .build()

        picasso.load(imgURL)
            .fit()
            .into(imgHolder)
Parik answered 4/7, 2021 at 15:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.