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

AndroidImageMediastorePicasso

Android Problem Overview


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?

Android Solutions


Solution 1 - Android

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) {
                }
            })

Solution 2 - Android

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

Solution 3 - Android

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)

Solution 4 - Android

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

Solution 5 - Android

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)

Solution 6 - Android

Please try this to check logs of the Picasso

Picasso.with(getContext()).setLoggingEnabled(true);

Solution 7 - Android

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)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionLittleFunnyView Question on Stackoverflow
Solution 1 - AndroidKevin van MierloView Answer on Stackoverflow
Solution 2 - AndroidTVT. JakeView Answer on Stackoverflow
Solution 3 - AndroiddonfuxxView Answer on Stackoverflow
Solution 4 - AndroidviperView Answer on Stackoverflow
Solution 5 - AndroidRajeev ShettyView Answer on Stackoverflow
Solution 6 - Androidharishanth raveendrenView Answer on Stackoverflow
Solution 7 - AndroidNeyomalView Answer on Stackoverflow