android – Glide 4.3.1: how to use error()?-ThrowExceptions

Exception or error:

First, what is the difference between Glide and GlideApp? The latter seems to be automatically generated, but the steps seemed to be complicated so I used Glide instead. They both seem to have the same methods.

So, I used Glide.with(activity).load(fileName).error().into(imageView). The problem is that I cannot understand what to pass to error(). It did not take a drawable resource ID. Android Studio says the parameter is RequestBuilder< Drawable!>?, but I could not find any example.

“Error: Type mismatch: inferred type is Int but RequestBuilder< Drawable!>? was expected”

How to solve:

If you are using Glide v4 then you have to use RequestOptions for including the more options you want, for example
centerCrop(), placeholder(), error(), priority() , diskCacheStrategy().

So after using RequestOptions your Glide would look like this-

RequestOptions options = new RequestOptions()
                    .centerCrop()
                    .placeholder(R.drawable.default_avatar)
                    .error(R.drawable.default_avatar)
                    .diskCacheStrategy(DiskCacheStrategy.ALL)
                    .priority(Priority.HIGH);

Glide.with(mContext).load(imgUrl)
                    .apply(options)
                    .into(picThumbnail);

Now you can show error image and placeholder set the disk cache etc.

GlideApp is also a part of Glide v4. It is used to provide more than one Transformation in Glide v4, using the transforms() method:

GlideApp.with(mContext)
  .load(imgUrl)
  .transforms(new CenterCrop(), new RoundedCorners(20))
  .into(target);

error() and placeholder() using GlideApp

GlideApp.with(mContext)
            .load(imageUrl)
            .placeholder(R.drawable.placeholder_image)
            .error(R.drawable.error_image)
            .diskCacheStrategy(DiskCacheStrategy.ALL)
            .priority(Priority.HIGH)
            .into(offerImage);

###

To answer your second question and the one is part of the title, you really only should care about error() if you either expect the url you are calling or drawable/bitmap is null and you may want to invoke a retry, otherwise you can use the error drawable to signal a different state compared to placeholder or fallback. The following chart explains it quite straight-forward:
Diagram showcasing the three possible fail cases of a Glide call
Unfortunately I cannot remember the original source of the image. Please let me know in the comments if you know so I can properly attribute it to the original author!

As you can see from the chart above as well, it is really enough to only set a placeholder if you don’t want to represent different states for each of them.

Hope the decision tree clears out some of your questions! Apologies that I have not answered all parts of your questions, but some of the other answers do that already!

Cheers!

###

Glide wants you to pass which image drawable you should show if an error occurs while loading it. This parameter is not required unless you like it. I typically just use.

 Glide.with(mContext).load(myModel.getDefaultThumbnailUrl()).dontAnimate().centerCrop().override(mImageSize, mImageSize).into(itemViewHolder.imgHolder);

Also if you want callbacks on the glide for error occurred or completed you can create a listener like this.

 RequestListener<String, GlideDrawable> glideCallback = new RequestListener<String, GlideDrawable>(){
        @Override
        public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
            Toast.makeText(myActivity.this, getString(R.string.error_loading_from_url), Toast.LENGTH_LONG).show();
            return false;

        }
        @Override
        public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    convertCardToBitmap(mSendWhenComplete);

                }
            },500);

            return false;

        }

    };

then include the .listener(glideCallback) in your chained load call.

error overloads include:

enter image description here

###

You specify a drawable in error() and if Glide throw an exception, this image is showed like a dummy image. The image specified in error() method would be a resource of your proyect.

For example:

Glide.with(this)
            .load(urlImage)
            .error(R.drawable.error_image).into(new GlideDrawableImageViewTarget(imageView) {
                @Override
                public void onResourceReady(GlideDrawable drawable, GlideAnimation anim) {
                    super.onResourceReady(drawable, anim);
                }
            });

error() : Error Drawables are shown when a request permanently fails. Error
Drawables are also shown if the requested url/model is null and no
fallback Drawable is set

GlideApp.with(fragment)
  .load(url)
  .error(R.drawable.error)
  .into(view);

or

GlideApp.with(fragment)
  .load(url)
  .error(new ColorDrawable(Color.RED))
  .into(view);

Leave a Reply

Your email address will not be published. Required fields are marked *