Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Glide 4.3.1: how to use error()? [duplicate]

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"

like image 676
Damn Vegetables Avatar asked Dec 20 '17 16:12

Damn Vegetables


2 Answers

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);
like image 79
D_Alpha Avatar answered Nov 11 '22 10:11

D_Alpha


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!

like image 30
anthonymonori Avatar answered Nov 11 '22 10:11

anthonymonori