使用Picasso库加载https网址上的图片时出现错误

3

在我的应用程序中,我正在尝试使用Picasso库从https URL加载图像。但是我无法将图像加载到ImageView中。我正在使用Picasso 2.5.2版本,并按以下方式设置图像:

  Picasso.with(mContext)
            .load(GlobalVariable.movieDetails.get(0).getVideo_cover_image())
            .placeholder(R.drawable.animation_placeholder)
            .error(R.drawable.animation_placeholder)
            .into(iv_image);

1
你在浏览器中检查过你的URL吗? - Rahul Singh Chandrabhan
@RahulChandrabhan 是的,它在浏览器中显示。 - Rishikesh Rahi
记录日志。你也可以为Picasso附加一个监听器。如果可能,请提供图像链接。 - rupinderjeet
@rupinderjeet它显示了错误时的可绘制图像。 图像的网址:- https://192.168.0.149:9999/ucine/uploads/moviecoverimage/thumb/movie_image2018-01-31-07-12-08.jpg - Rishikesh Rahi
2
哇,那是一个本地链接,我无法访问它。在LogCat中,当您开始加载图像时,请检查警告消息,如果Picasso无法加载图像,则会输出一些警告。 - rupinderjeet
显示剩余2条评论
3个回答

1
尝试这个。
if (TextUtils.isEmpty(imageUrl) && !isValidUrl(imageUrl)) {
                iv_image.setImageResource(R.drawable.default);

            } else {
                Picasso.with(context)
                        .load(imageUrl)
                        .resize(70, 70)//if you want resize image
                        .centerInside()
                        .into(iv_image, new com.squareup.picasso.Callback() {
                            @Override
                            public void onSuccess() {

                            }

                            @Override
                            public void onError() {
                                iv_image.setImageResource(R.drawable.default);
                            }
                        });
            }

验证URL
 public static boolean isValidUrl(String string) {

    boolean b = Patterns.WEB_URL.matcher(string).matches();
    return b;
}

https://192.168.0.149:9999/ucine/uploads/moviecoverimage/thumb/movie_image2018-01-31-07-12-08.jpg这是本地URL的图片,因此需要在本地网络上运行应用程序。 - Adil
1
是的,这是本地URL并且我也在本地服务器上运行,使用Glide时它可以正常工作,但是使用Picasso时却无法工作。 - Rishikesh Rahi

0

Picasso提供了一个用于图片加载的监听器。

使用以下代码,可以检查图片加载状态,无论是成功还是错误。

 Picasso.with(mContext)
        .load(GlobalVariable.movieDetails.get(0).getVideo_cover_image())
        .placeholder(R.drawable.animation_placeholder)
        .error(R.drawable.animation_placeholder)
        .into(iv_imagenew, 
                    new com.squareup.picasso.Callback() {
                    @Override
                    public void onSuccess() {
                          //Image Loaded
                    }

                    @Override
                    public void onError() {
                         //Error while Loading
                    }
                });

0
请使用Picaso的resize(horizontalSize, verticalSize)方法来调整图像大小。也许您的图像尺寸太大了,所以它无法加载,请尝试使用resize(horizontalSize, verticalSize)进行调整。
Picasso.with(context)
       .load(GlobalVariable.movieDetails.get(0).getVideo_cover_image())
       .placeholder(R.drawable.animation_placeholder)
       .error(R.drawable.animation_placeholder)
       .into(iv_image);
       .resize(600, 200) // resizes the image to these dimensions (in pixel). does not respect aspect ratio
       .into(imageViewResize);

你能给我你的图片链接吗? - shubham chauhan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接