Issue/Question
I've been trying to implement basic image displaying with Glide, but it seems like it only returns the bitmap if the image passed to the ".load" method could be loaded successfully. Displaying the error drawable wouldn't cause Glide to notify the callback method defined in the RequestListener and passing the error bitmap, but the onException method (returning nothing valuable for me). For example this prevents me from being able to generate a Palette from an error image.
Glide Version:
How I added Glide to my project:
compile 'com.github.bumptech.glide:glide:3.7.0'
Additional dependencies
The library I use can be found here : https://github.com/wasabeef/glide-transformations
Device/Android Version
I've already tried the code on an emulated Nexus 6P (Android 6.0), emulated Nexus 5x (Android 6.0) and a real Samsung Galaxy S3 LTE running version Android 4.3.
What I do in the classes
First of all, sorry for the terrible formatting.
Glide
.with(getActivity())
.load(uri)
.asBitmap()
.transform(newCropCircleTransformation(getActivity()))
.error(R.drawable.movie_error_drawable)
.into(movieHolder.art);
CropCircleTransformation is a part of the library I mentioned above.
This is what I also tried as a temporary solution:
Glide
.with(getActivity())
.load(uri)
.asBitmap()
.transform(new CropCircleTransformation(getActivity()))
.listener(new RequestListener<Uri, Bitmap>() {
@Override
public boolean onException(Exception e, Uri model, Target<Bitmap> target, boolean isFirstResource) {
// I load the movie error drawable in the onException method,
// so even the error drawable will be rounded!
Glide
.with(getActivity())
.load(R.drawable.movie_error_drawable)
.asBitmap()
.transform(new CropCircleTransformation(getActivity()))
.into(movieHolder.art);
return false;
}
@Override
public boolean onResourceReady(Bitmap resource, Uri model, Target<Bitmap> target, boolean isFromMemoryCache, boolean isFirstResource) {
return false;
}
})
.into(movieHolder.art);
It was intended to be a really hackish way to get the error drawable bitmap. There are still problems even with this "solution". Sometimes the image, loaded in the onException method doesn't get transformed (I never get a rounded view) and in most cases it doesn't even get loaded!
Last words
How can I achieve the desired thing (without hacking)?
Lot of things were mentioned so I'll just write bullets:
.error or .placeholder as a Bitmap without hackingTarget interface (BitmapImageViewTarget is the impl you're getting) to see how the error (onLoadFailed) and placeholder (onLoadStarted/onLoadCleared) Drawable is being handled. RequestListener is just peeking at what's happening, the real deal is Target.onException if you understand this https://github.com/bumptech/glide/issues/913#issuecomment-181099468movieHolder.art.post(() -> Glide.with...). This may be needed because these interaction with the user (you) are on the UI thread, so ordering must be handled correctly.R.drawable.x and cache it, those values may only change per release. You can even persist the colors in DB or preferences (just don't forget to clear them on each release because IDs may change: SQLiteOpenHelper.onUpgrade is a good callback to clear in as it's called once per update, if you bump the DB version with the versionCode)Have a nice read of linked issues.
Thank you so much. I'll have to look after a lot of things, so I hope I can create an acceptable solution.
Feel free create new issues if you have specific problems that are not covered by the links.
Most helpful comment
Lot of things were mentioned so I'll just write bullets:
.erroror.placeholderas a Bitmap without hackingTargetinterface (BitmapImageViewTargetis the impl you're getting) to see how the error (onLoadFailed) and placeholder (onLoadStarted/onLoadCleared) Drawable is being handled.RequestListeneris just peeking at what's happening, the real deal isTarget.onExceptionif you understand this https://github.com/bumptech/glide/issues/913#issuecomment-181099468But, you may be able to get it work if you return true after the load. If that doesn't work either you can try to post a new load with
movieHolder.art.post(() -> Glide.with...). This may be needed because these interaction with the user (you) are on the UI thread, so ordering must be handled correctly.R.drawable.xand cache it, those values may only change per release. You can even persist the colors in DB or preferences (just don't forget to clear them on each release because IDs may change:SQLiteOpenHelper.onUpgradeis a good callback to clear in as it's called once per update, if you bump the DB version with the versionCode)Have a nice read of linked issues.