Picasso doesn't tolerate empty String URL?

一世执手 提交于 2020-02-03 05:40:07

问题


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?


回答1:


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.




回答2:


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



回答3:


I hope it helps you:

if (item.getImagen().isEmpty()) { //url.isEmpty()
        Picasso.with(mContext)
                .load(R.drawable.placeholder)
                .placeholder(R.drawable.placeholder)
                .error(R.drawable.placeholder)
                .into(holder.imageView);

    }else{
        Picasso.with(mContext)
                .load(item.getImagen())
                .placeholder(R.drawable.placeholder)
                .error(R.drawable.placeholder)
                .into(holder.imageView); //this is your ImageView
    }



回答4:


I suggest you check the string before loading into Picasso.

public static boolean isBlank(String string) {
    return TextUtils.isEmpty(string.trim());
}



回答5:


Warning: Its not enough to check the emptiness of the url String

You should trim the url String first and then do the empty check. Otherwise a String like " " can break your app.

This is how I use it.

if (imageUrl != null && imageUrl.trim().isEmpty())
{
    imageUrl = null;
}

Picasso.with(mContext)
    .load(imageUrl) // its safe to pass null, but not "" or " "
    .placeholder(R.drawable.placeholder)
    .into(mImageView);

Check the source code of Picasso to learn why.

Discussions on the topic: https://github.com/square/picasso/issues/609




回答6:


You can check if url is empty by using below method

if(!TextUtils.isEmpty(url.trim()) && url.trim().length >0){
    if(Patterns.WEB_URL.matcher(url).matches()){

    }
}

Source Url validation : How to check if URL is valid in Android



来源:https://stackoverflow.com/questions/36538194/picasso-doesnt-tolerate-empty-string-url

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!