【问题标题】:Picasso ImageError毕加索图像错误
【发布时间】:2020-01-09 04:27:16
【问题描述】:

我正在使用 Picasso 在我的 android 应用程序中显示来自服务器的图像。我有 5 个图像 URL(HTTP 表单)从服务器获取并将其存储在字符串值中。如果我向 Picasso 发送正确的链接(.jpg 表单)它运行正确并在我的图像视图中显示我的图像,如果以(.pdf 格式)发送错误的链接,它会在我的图像视图中显示错误,但是当我从服务器传递空值或空值到字符串时,我的应用程序崩溃它的运行if 语句首先,即使它的值为 null 或空 else 语句没有运行我应该在我的代码中更新什么,以便如果我从服务器获取 null 值,我的 imageview 应该显示并且错误和文本视图值应该被更改。

// 仅在我的 If/Else 开始处编写代码:

    if (image_fourth != null && image_fourth != ""){
        Picasso.get().load(image_fourth).resize(200, 200).placeholder(R.drawable.placeholder).error(R.drawable.error).into(image1);
        image1.setVisibility(View.VISIBLE);

        buttons.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (image_second == null){
                    image_2t.setText("Image Not Found");
                    image_2t.setVisibility(View.GONE);


                }
                else if (image_second != null){
                    Picasso.get().load(image_second).resize(200, 200).placeholder(R.drawable.placeholder).error(R.drawable.error).into(image2);
                    image2.setVisibility(View.VISIBLE);
                    image_2t.setText("Image 2");
                    image_2t.setVisibility(View.VISIBLE);
                }
            }
        });
    }
    else{
        image_1t.setText("Image Not Found");
    }

【问题讨论】:

  • 会出现什么类型的错误?
  • @BharatVankar 如果我的字符串为空,则它指向“Picasso.get().load(image_fourth).resize(200, 200).placeholder(R.drawable.placeholder).error( R.drawable.error).into(image1); "

标签: android android-studio picasso


【解决方案1】:

使用 Glide 代替 Picasso,则不需要添加任何其他条件或代码来检查字符串是否为空, 检查此示例

Glide.with(activity_context)
                .load(your_url)
                .placeholder(R.drawable.default_image)
                .error(R.drawable.default_image)
                .override(200, 200)
                .centerCrop()
                .dontAnimate()
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .into(mHolder.imgIcon); 

你可以找到更多关于 Glide here

如果您想继续使用毕加索,请尝试此代码,这可能会对您有所帮助

if(!TextUtils.isEmpty(Url)) {

            Picasso.with(activity).load(Url.replace(" ", "%20")).error(R.drawable.default_image).networkPolicy(NetworkPolicy.NO_CACHE)
                    .memoryPolicy(MemoryPolicy.NO_CACHE)

                    .into(imageView, new Callback() {

                        public void onSuccess() {
                            System.out.println(" 02062015:onSuccess");


                        }

                        @Override
                        public void onError() {
                            imageView.setImageResource(R.drawable.default_image);
                            System.out.println(" 02062015:onError");

                        }
                    });
        }

【讨论】:

    【解决方案2】:

    您可以尝试颠倒 if/else 语句。据我所知,毕加索不能在 load() 中使用 null 或空字符串。在您的情况下,如果字符串/url 源为空,则 if 语句可以覆盖您可以加载占位符:

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

    另一种选择可能是保持 if 语句不变,但将其添加到 else 语句中:

    else
            image_fourth.setImageResources(R.mipmap.ic_launcher);
    

    我在 Glide 中使用了类似的技术。这对我有用。有关更多信息,您可以查看此 stackoverflow 答案:Picasso doesn't tolerate empty String URL?

    【讨论】:

    • 先生 thnku 的帮助可以帮助我解决我面临的这个新问题吗?问题链接:stackoverflow.com/questions/59713938/…
    • 不幸的是,我对 Volley 了解不多,所以我不确定能否帮助您解决下一个问题
    【解决方案3】:

    只需在语句之间切换并检查它是否有效?

    if (image_fourth == null || image_fourth == ""){
    //write your else statement here
    }else{
    //And if code is here
    }
    

    【讨论】:

      猜你喜欢
      • 2016-12-12
      • 2016-04-07
      • 2017-09-18
      • 2014-08-29
      • 2014-04-10
      • 2014-09-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多