【问题标题】:Picasso doesn't tolerate empty String URL?Picasso 不能容忍空字符串 URL?
【发布时间】:2016-07-31 23:53:17
【问题描述】:

我有一个使用 Picasso 加载图像的 viewHolder。数据库将返回 URL 的路径作为字符串。所以我的代码如下(使用 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 字符串为空而不是加载占位符时崩溃吗?

【问题讨论】:

  • 是的,为空字符串加载 Picasso(相对而言)会产生很多开销。
  • 如果字符串为空或 url 不可访问,毕加索不会在内部处理,它应该回退到“错误”或“占位符”图像吗?
  • 可以,但有时库更容易假设您将使用适当的值,而不是有时在任何地方进行检查。让用户承担一些责任。
  • @zgc7009,当你说“它可以”时,也许你可以和我分享一下......我想把它放进去,以消除我的额外检查。所以我不需要担心数据库是如何传递价值的。如果它们有效,我将发布图像,否则我将退回到错误图像。谢谢。
  • 不,我是说他们可以这样设计,但不是那样设计的。你必须给它一个字符串。不幸的是,有时检查只是其中的一部分

标签: android picasso


【解决方案1】:

javadoc for Picasso.load() 明确声明当 URL 为 null 或为空时,它将引发 IllegalArgumentException。所以这就是你可以期待的。

【讨论】:

  • 谢谢。生活的事实。我也许可以添加 Try-Catch 来处理它。
  • 如果您只是自己检查空或 null 并采取适当的措施,它将更加高效和可读。在java中这是一件很常见的事情。
  • 酷。就像我上面做的那样。只是想以防万一我可能会错过其他一些错误情况……因为我无法完全控制数据库,所以可能是其他可能导致毕加索抛出异常的事情。 p/s:在我的程序中,如果输入无效,那么我将忽略它并显示占位符图像。 ... 顺便说一句,我喜欢你的 Kotlin 博客。很酷的帖子!
  • 谢谢!很高兴你从中有所收获。 :-)
  • 事实上,传递 null 作为路径不会触发任何请求,但会设置一个占位符,如果指定了一个。仅当路径为空或空字符串时才抛出 IllegalArgumentException。
【解决方案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
    }

【讨论】:

  • 尽量不要发布仅包含代码的答案。几行解释使这样的答案一目了然更容易理解,更容易被赞成/接受。
  • 使用 TextUtils.IsEmpty (str) 然后 str.IsEmpty() 更安全,因为它检查 null 而不是在 str 为 null 时抛出异常
  • .equals("") 在我的情况下工作,但无论如何,这个解决方案是黄金。
【解决方案3】:

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

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

【讨论】:

    【解决方案4】:

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

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

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

    【讨论】:

      【解决方案5】:

      警告:检查 url 字符串是否为空是不够的

      您应该先修剪 url 字符串,然后再进行空检查。否则像" " 这样的字符串可能会破坏您的应用程序。

      这就是我使用它的方式。

      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

      【讨论】:

        【解决方案6】:

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

        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

        【讨论】:

          【解决方案7】:

          是的,毕加索需要非 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);
                  }
          

          【讨论】:

            【解决方案8】:

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

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

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2018-09-08
              • 2015-01-03
              • 1970-01-01
              • 2017-05-18
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2019-05-03
              相关资源
              最近更新 更多