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);
}
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.
What i want to achieve is:
if(Image Link exist){
//Load Image, Picasso.with.................
} else {
//Use Dummy Photo, Picasso.with..................
}
[Edit] 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??
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.
You can use com.squareup.picasso.Callback
to listen for response.:
Picasso.with(getContext())
.load(url)
.placeholder(R.drawable.image_name) // Your dummy image...
.into(imageView, new com.squareup.picasso.Callback() {
@Override
public void onSuccess() {
// Image is loaded successfully...
}
@Override
public void onError() {
// Unable to load image, may be due to incorrect URL, no network...
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With