问题
I have loaded remote image to ImageView
using below code
Glide.with(context)
.load(imageUrl)
.placeholder(R.drawable.placeholderImage)
.into(holder.wallPaperImageView);
Now I want to get the bitmap from imageview
Bitmap bitmap = ((GlideBitmapDrawable)holder.wallPaperImageView.getDrawable()).getBitmap();
but above line of code throws below exception:
java.lang.ClassCastException: android.graphics.drawable.TransitionDrawable cannot be cast to com.bumptech.glide.load.resource.bitmap.GlideBitmapDrawable
How to resolve this?
回答1:
Use this instead of that:
Bitmap bmp = ((GlideBitmapDrawable)holder.wallPaperImageView.getDrawable().getCurrent()).getBitmap();
or
Glide
.with(this)
.load(a.getAlbum_artwork())
.asBitmap()
.into(new SimpleTarget<Bitmap>(300,300) {
@Override
public void onResourceReady(Bitmap resource, GlideAnimation glideAnimation) {
setBackgroundImage(resource);
}
});
回答2:
If you wanna get away from resizing use this (talisman):
Glide.with(context)
.load(imageUrl)
.asBitmap() //needed for obtaining bitmap
.placeholder(R.drawable.placeholderImage)
.into(holder.wallPaperImageView);
Then you are free to get bitmap:
Bitmap bitmap = ((BitmapDrawable)holder.wallPaperImageView.getDrawable()).getBitmap();
It's working with Glide 3.8.0
Good luck,'.
回答3:
If you use Glide v4
private BaseTarget target = new BaseTarget<BitmapDrawable>() {
@Override
public void onResourceReady(BitmapDrawable bitmap, Transition<? super BitmapDrawable> transition) {
//here is your integration by ex: activityReference.get().toolbar.setNavigationIcon(bitmap);
}
@Override
public void getSize(SizeReadyCallback cb) {
cb.onSizeReady(100, 100);
}
@Override
public void removeCallback(SizeReadyCallback cb) {}
};
and after
Glide.with(activityReference.get().getApplicationContext())
.load(url of image)
.apply(new RequestOptions()
.placeholder(R.mipmap.img)
.error(R.mipmap.img)
.diskCacheStrategy(DiskCacheStrategy.ALL))
.apply(circleCropTransform())
.into(target);
if you have warning 'Unchecked method ...' add before method @SuppressWarnings("unchecked")
来源:https://stackoverflow.com/questions/37701061/android-graphics-drawable-transitiondrawable-cannot-be-cast-to-com-bumptech-glid