Glide - Call Method After Fallback Or Error When Trying Load Photo
Glide - call method after fallback or error when trying load photo. Hi! Is there any a way to check if Glide load photo from link or use fallback/error when link isn't valid or pho
Solution 1:
This helps me:
privatevoidloadPicture(final ViewHolder holder, String photoUrl, final Boolean shouldLoadAgain) {
holder.progressBar.setVisibility(View.VISIBLE);
Glide
.with(mActivity)
.load(photoUrl)
.fallback(R.drawable.bg_gradient)
.error(R.drawable.bg_gradient)
.centerCrop()
.crossFade()
.listener(newRequestListener<String, GlideDrawable>() {
@OverridepublicbooleanonException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
holder.progressBar.setVisibility(View.GONE);
if (shouldLoadAgain)
loadPicture(holder, mPhotoUrl, false);
returnfalse;
}
@OverridepublicbooleanonResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
holder.progressBar.setVisibility(View.GONE);
returnfalse;
}
})
.diskCacheStrategy(DiskCacheStrategy.SOURCE)
.into(holder.photo);
}
Solution 2:
This is working for me. For some reason, if I don't use postDelayed on load failed, the app is crashing.
privatevoidupdateImage(final String image) {
Glide
.with(this)
.load(image)
.apply(newRequestOptions()
.placeholder(R.drawable.bg_gradient)
.error(R.drawable.bg_gradient))
.listener(newRequestListener<Drawable>() {
@OverridepublicbooleanonLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
newHandler().postDelayed(newRunnable() {
@Overridepublicvoidrun() {
updateImage(image);
}
}, 1000);
returnfalse;
}
@OverridepublicbooleanonResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
returnfalse;
}
})
.into(holder.photo);
}
Post a Comment for "Glide - Call Method After Fallback Or Error When Trying Load Photo"