Android Picasso在加载到imageView之前检查图像URL是否存在 [英] Android picasso check if image url exist before load into imageView

查看:144
本文介绍了Android Picasso在加载到imageView之前检查图像URL是否存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用具有数据绑定工具的RecyclerView,以从从API获取的url列表(/images/slider/my/myImage.jpg)中加载图片.

I'm currently working on RecyclerView with data binding implement, to load Image from a list of url (/images/slider/my/myImage.jpg) get from API.

@BindingAdapter("imageUrl")
public static void loadImage(ImageView imageView, String imageUrl){
    Picasso.with(imageView.getContext()).load(CommUtils.WEBSITE_LINK + imageUrl).into(imageView);
}

当前,我的ListAdapter中有上面的代码.当网址位于正确的链接中或存在于服务器中时,能够很好地加载图像的代码将其保留为空白.因此,我想创建一个案例,以便在显示之前检查图像是否存在/是否正确.

Currently i have the code above in my ListAdapter. The code able to load the image fine when the url is in correct link or exist in server else it will it as blank. Therefore, i want to create a case that will check if the image is exist/is correct link before display.

我想要实现的是:

if(Image Link exist){
//Load Image, Picasso.with.................
} else {
//Use Dummy Photo, Picasso.with..................
}

现在,我知道如果路径不存在,我可以使用error()函数创建另一个负载.如果我的API返回两种可能不同的格式或"url",该怎么办?有路径(/images/slider/my/myImage.jpg)或无路径(myImage.jpg) 因此,在我的代码中,我想做类似的事情

So now i know i can use the error() function to create another load if the path is not exist. How about when in a case that my API will return two difference format or "url" which could be; with path (/images/slider/my/myImage.jpg) or without path (myImage.jpg) Therefore in my code i want to do something like

if(websitelink + ImageUrl){ load image }
else(websitelink + path + ImageUrl) { load iamge} //Should this code run under error() from the first case??

我可以先在ImageUrl上执行检查,而不是尝试直接加载图像并且仅在出现错误时更改

Can i perform a checking on the ImageUrl first instead of try to load the image directly and only change when is error

推荐答案

毕加索始终支持占位符和错误图像:

Picasso supports placeholder and error images anyways:

Picasso.get()
    .load(url)
    .placeholder(R.drawable.user_placeholder)
    .error(R.drawable.user_placeholder_error)
    .into(imageView);

因此,如果您要归档的只是显示一些错误图像,则在加载不起作用时,这就是您所需要的.

So, if all you want to archieve, is to show some error image, when loading does not work, that is all you need.

这篇关于Android Picasso在加载到imageView之前检查图像URL是否存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆