毕加索不容忍空字符串网址吗?

时间:2016-04-11 00:51:55

标签: android picasso

我有一个使用Picasso加载图像的viewHolder。 DB将返回URL的路径作为String。所以我的代码如下(使用Kotlin)

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

加载很好。但是,有时URL是空的。我希望它能加载占位符。但它崩溃如下

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

这会迫使我明确地进行检查,这是不理想的

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

当URL String为空而不是加载占位符时,预计Picasso会崩溃吗?

8 个答案:

答案 0 :(得分:3)

javadoc for Picasso.load()明确声明当URL为null或为空时,它将抛出IllegalArgumentException。这就是你所期望的。

答案 1 :(得分:2)

我希望它可以帮到你:

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
    }

答案 2 :(得分:1)

这可能为时已晚,但我今天遇到此错误,在阅读Picasso#load方法的文档后,它声明传递空字符串或空字符串将导致方法抛出IllegalArgumentException并传递null将不抛出异常但触发RequestCreator#error,如果提供了错误图像,则会加载错误图像,否则目标将不会显示任何内容。

如果您无法控制图片网址(例如来自服务器),您可以尝试以下操作:

 mPicasso.load(photo.isEmpty() ? null : photo)
                .placeholder(placeholder)
                .error(error_placeholder)
                .into(target);

答案 3 :(得分:0)

我建议您在加载到Picasso之前检查字符串。

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

答案 4 :(得分:0)

警告:它不足以检查url String

的空虚

您应首先修剪url String,然后执行空检查。否则,像" "这样的字符串可能会破坏您的应用。

这就是我使用它的方式。

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

检查毕加索的源代码以了解原因。

讨论该主题:https://github.com/square/picasso/issues/609

答案 5 :(得分:0)

您可以使用以下方法检查网址是否为空

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

    }
}

源网址验证:How to check if URL is valid in Android

答案 6 :(得分:0)

是的,毕加索期望非null和非空值,因此我建议使用助手TextUtils来处理此问题。 这里举个例子。

在此处输入代码


 String imagenUser = listaComentarios.get(position).getAvatar();

   if (TextUtils.isEmpty(imagenUser)) {
            Toast.makeText(context, "Avatar imagenes" ,Toast.LENGTH_LONG).show();
            holder.imagen_usuario.setImageResource(R.mipmap.ic_launcher);
        } else {
                    Picasso.get().load(imagenUser)
                    .fit()
                    .centerCrop()
                    .transform(new CircleTransform())
                    .into(holder.imagen_usuario);
        }

答案 7 :(得分:0)

很晚了,但我遇到了同样的问题,我通过传递 Uri 而不是 url 来解决。只需像 Uri.parse(url) 一样传递您的网址。
它会是这样的

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