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"
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:
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With