7

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 photo isn't available?

I mean, I want to call a method (load other photo) when Glide doesn't load photo.

This is my Glide e.g.:

Glide
        .with(mActivity)
        .load(news.getPagemap().getCseThumbnail().get(0).getSrc())
        .fallback(R.drawable.bg_gradient)
        .error(R.drawable.bg_gradient)
        .centerCrop()
        .crossFade()
        .diskCacheStrategy(DiskCacheStrategy.SOURCE)
        .into(holder.photo);

I tried to compare ConstantValues - holder.photo.getDrawable().getConstantState().equals(mActivity.getResources().getDrawable(R.drawable.bg_gradient).getConstantState()) but got NullPointerException.

y07k2
  • 1,898
  • 4
  • 20
  • 36

2 Answers2

12

This helps me:

private void loadPicture(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(new RequestListener<String, GlideDrawable>() {
            @Override
            public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
                holder.progressBar.setVisibility(View.GONE);
                if (shouldLoadAgain)
                    loadPicture(holder, mPhotoUrl, false);
                return false;
            }

            @Override
            public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
                holder.progressBar.setVisibility(View.GONE);
                return false;
            }
        })
        .diskCacheStrategy(DiskCacheStrategy.SOURCE)
        .into(holder.photo);
}
y07k2
  • 1,898
  • 4
  • 20
  • 36
  • 1
    Thanks for this, i was using picasso to do the same thing (try load image using one url and if it fails, use another) but it kept giving me out of memory errors. no errors using glide! – Clive Sargeant Sep 06 '16 at 15:19
5

This is working for me. For some reason, if I don't use postDelayed on load failed, the app is crashing.

private void updateImage(final String image) {
    Glide
            .with(this)
            .load(image)
            .apply(new RequestOptions()
                    .placeholder(R.drawable.bg_gradient)
                    .error(R.drawable.bg_gradient))
            .listener(new RequestListener<Drawable>() {

                @Override
                public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            updateImage(image);
                        }
                    }, 1000);
                    return false;
                }

                @Override
                public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
                    return false;
                }
            })
            .into(holder.photo);
}
Sethuraman Srinivasan
  • 1,528
  • 1
  • 20
  • 34