Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Glide use default image when onLoadFailed()

I use glide version 4.7.1 I wanna use default image when onLoadFailed.

this is my code

Glide.with(context).load(imageUrl).listener(new RequestListener<Drawable>() {
    @Override
    public boolean onLoadFailed(@Nullable GlideException e, Object model, com.bumptech.glide.request.target.Target<Drawable> target, boolean isFirstResource) {
        // fail
        // How can i use default image in imgvAssetPicture?
    }

    @Override
    public boolean onResourceReady(Drawable resource, Object model, com.bumptech.glide.request.target.Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
        // success
        imgvLoadingProgressbar.setVisibility(View.INVISIBLE);
        imgvAssetPicture.setVisibility(View.VISIBLE);
        return false;
    }
}).into(imgvAssetPicture);
like image 856
SAWD Avatar asked Sep 01 '26 10:09

SAWD


1 Answers

 Glide.with(passContext)
.applyDefaultRequestOptions(new RequestOptions()
.placeholder(R.drawable.ic_user_default)
.error(R.drawable.ic_user_default))
.load(url)
.into(image);

With version 4.7.1 (you are using), you can easily set these options.

  • placeHolder which shows when there is not image.
  • error when some URL fails to load.

Bonus

Are you using some ProgressBar with setting visibility, that's very old way to do.

See CircularProgressDrawable, which is very easy to use. Just pass this CircularProgressDrawable in your placeHolder.

like image 97
Khemraj Sharma Avatar answered Sep 03 '26 02:09

Khemraj Sharma



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!