Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Picasso doesn't tolerate empty String URL?

I have a viewHolder that loads up an image using Picasso. The DB will return a path of URL as String. So I have my code as below (Using Kotlin)

  Picasso.with(context).load(url).error(placeholder).transform(transformation)
            .placeholder(placeholder).into(this)

It loads fine. However, sometimes the URL is empty. I'm expecting it to load the placeholder instead. But it crash out as below

java.lang.IllegalArgumentException: Path must not be empty.
    at com.squareup.picasso.Picasso.load(Picasso.java:297)

This would force me to explicitly do a check, which is not ideal

if (url == null || url.isEmpty()) {
    Picasso.with(context).load(placeholder).transform(transformation).into(this)
} else {
    Picasso.with(context).load(url).error(placeholder).transform(transformation)
            .placeholder(placeholder).into(this)
}

Is this expected that Picasso will crash when a URL String is empty instead of loading the placeholder?

like image 878
Elye Avatar asked Apr 11 '16 00:04

Elye


2 Answers

This might be too late but i encountered this error today and after read the documentation of Picasso#load method it states that passing empty or blank string will cause the method to throw IllegalArgumentException and passing a null will not throw an exception but trigger RequestCreator#error which will load error image if one is provided otherwise the target will display nothing.

if you have no control over the image url (say its coming from server) you can try the following:

 mPicasso.load(photo.isEmpty() ? null : photo)
                .placeholder(placeholder)
                .error(error_placeholder)
                .into(target);
like image 121
Mohammed Elrashied Avatar answered Sep 28 '22 13:09

Mohammed Elrashied


The javadoc for Picasso.load() explicitly states that it will throw an IllegalArgumentException when the URL is null or empty. So that's what you can expect.

like image 43
Doug Stevenson Avatar answered Sep 28 '22 12:09

Doug Stevenson