Casting from RoundedBitmapDrawable to BitmapDrawable - android

Everything worked well before starting using RoundedBitmapDrawable to round Bitmap's corners.
After starting using RoundedBitmapDrawable, I'm getting:
java.lang.ClassCastException:
android.support.v4.graphics.drawable.RoundedBitmapDrawable21 cannot be cast to android.graphics.drawable.BitmapDrawable
Code:
BitmapDrawable bitmapDrawable = ((BitmapDrawable) imageView.getDrawable());

There is not a hierarchical link between these two types except that they share the same parent. Instead, you can check the type of drawable at runtime and cast accordingly,
if (imageView.getDrawable() instanceof android.support.v4.graphics.drawable.RoundedBitmapDrawable) {
RoundedBitmapDrawable drawable = (RoundedBitmapDrawable) imageView.getDrawable();
Bitmap roundedBitmap = drawable.getBitmap();
// ...
}

Related

How to save RoundedBitmapDrawable

I want to change a shape of image from square to round. And after that I am planning to save this bitmap. But i faced with problem I can't cast RoundedBitmapDrawable to Bitmap? Or may be exist another way for saving RoundedBitmapDrawable instance to SD card.
void setImage(){
mBitmap = BitmapFactory.decodeResource(getResources(),R.drawable.user);
mImageView.setImageDrawable(createNewBitmap(mBitmap));
mBitmap = ((BitmapDrawable) mImageView.getDrawable()).getBitmap();
//after that i am planning save bitmap
}
Drawable createNewBitmap(Bitmap bitmap){
RoundedBitmapDrawable roundedBitmapDrawable = RoundedBitmapDrawableFactory.create(getResources(),bitmap);
roundedBitmapDrawable.setCornerRadius(bitmapRadius);
roundedBitmapDrawable.setAntiAlias(true);
return roundedBitmapDrawable;
}

How to get bitmap from NetworkImageView?

In my project I used (Volley + NetworkImageView) to download some images and texts and showing them in a list view.. till here, I don't have any problem.
Now, I want to get bitmaps form NetworkImageView and I tried many methods like the following, but non of them worked for me.
BitmapDrawable drawable = (BitmapDrawable) imageView.getDrawable();
Bitmap bitmap = drawable.getBitmap();
Another method:
imageView.buildDrawingCache();
Bitmap bmap = imageView.getDrawingCache();
Non of them worked..
Any Help is appreciated,,
You cannot get the Bitmap reference as it is never saved in the ImageView.
however you can get it using :
((BitmapDrawable)this.getDrawable()).getBitmap();
beacuse when you set it with Volley u do this:
/**
* Sets a Bitmap as the content of this ImageView.
*
* #param bm The bitmap to set
*/
#android.view.RemotableViewMethod
public void setImageBitmap(Bitmap bm) {
// Hacky fix to force setImageDrawable to do a full setImageDrawable
// instead of doing an object reference comparison
mDrawable = null;
if (mRecycleableBitmapDrawable == null) {
mRecycleableBitmapDrawable = new ImageViewBitmapDrawable(
mContext.getResources(), bm);
} else {
mRecycleableBitmapDrawable.setBitmap(bm);
}
setImageDrawable(mRecycleableBitmapDrawable);
}
however if you set your default image or error image or any other image in any other way you may not get BitmapDrawable but NinePatchDrawable for example.
here is how to check:
Drawable dd = image.getDrawable();
if(BitmapDrawable.class.isAssignableFrom(dd.getClass())) {
//good one
Bitmap bb = ((BitmapDrawable)dd).getBitmap();
} else {
//cannot get that one
}

How to get the whole bitmap attached to an ImageView?

I tried to get Bitmap attached to an ImageView, using ImageView.getDrawingCache(); But I found that the returned Bitmap was not the same as I'd like to get from the ImageView. It was always smaller than the real image.
I had known that, the method getDrawingCache() should not have the view if it is bigger than the screen as the visible portion of the view is only drawn and the cache holds only what is drawn.
Could I get the whole bitmap attached to a ImageView?
If you just want the Bitmap from a ImageView the following code may work for you:-
Bitmap bm=((BitmapDrawable)imageView.getDrawable()).getBitmap();
I think that's what you wanted.
If your drawble is not always an instanceof BitmapDrawable
Note: ImageView should be set before you do this.
Bitmap bitmap;
if (mImageView.getDrawable() instanceof BitmapDrawable) {
bitmap = ((BitmapDrawable) mImageView.getDrawable()).getBitmap();
} else {
Drawable d = mImageView.getDrawable();
bitmap = Bitmap.createBitmap(d.getIntrinsicWidth(), d.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
d.draw(canvas);
}
Your bitmap is stored in bitmap.
Voila!
Easiest way is to set tag in ImageView.
imageView.setImageBitmap(bitmap);
imageView.setTag(bitmap);
To get Tag from it
imageView.getTag();

How to add a Drawable object to a Parcel object in android?

I'm facing problem in adding a drawable object to a Parcel object, in order to send an object from one activity to another activity.
There are methods like writeString(String) to add strings to a Parcel object. But do not know how to add a Drawable to the Parcel object.
You cannot add a Drawable to a Parcel, as Drawable does not implement the Parcelable interface. Certain types of Drawable might implement Parcelable, but I am not aware of any.
You can put in the Parcel some identifier (e.g., drawable resource ID) and have the recipient obtain the Drawable on its own.
I think you can try wrapping the Drawable inside an object that implements Parcelable.
You can do that using Bitmap and BitmapDrawable.
Let's say you have a Drawable called drawable and a Parcel (in which the object should be written) called parcel.
To write a Drawable into a Parceable:
if ( drawable != null ) {
Bitmap bitmap = (Bitmap) ((BitmapDrawable) drawable).getBitmap();
parcel.writeParcelable(bitmap, flags);
}
else {
parcel.writeParcelable(null, flags);
}
To read the Drawable from the Parceable:
Bitmap bitmap = (Bitmap) in.readParcelable(getClass().getClassLoader());
if ( bitmap != null ) {
drawable = new BitmapDrawable(bitmap);
}
else {
drawable = null;
}
In particular, since the constructor BitmapDrawable (Bitmap bitmap) has been deprecated, you might want to use BitmapDrawable (Resources res, Bitmap bitmap) instead.
drawable = new BitmapDrawable(context.getResources(), bitmap);

How to convert a Bitmap to Drawable in android?

How can I convert a Bitmap image to Drawable ?
Try this it converts a Bitmap type image to Drawable
Drawable d = new BitmapDrawable(getResources(), bitmap);
Sounds like you want to use BitmapDrawable
From the documentation:
A Drawable that wraps a bitmap and can
be tiled, stretched, or aligned. You
can create a BitmapDrawable from a
file path, an input stream, through
XML inflation, or from a Bitmap
object.
Having seen a large amount of issues with bitmaps incorrectly scaling when converted to a BitmapDrawable, the general way to convert should be:
Drawable d = new BitmapDrawable(getResources(), bitmap);
Without the Resources reference, the bitmap may not render properly, even when scaled correctly. There are numerous questions on here which would be solved simply by using this method rather than a straight call with only the bitmap argument.
Offical Bitmapdrawable documentation
This is sample on how to convert bitmap to drawable
Bitmap bitmap;
//Convert bitmap to drawable
Drawable drawable = new BitmapDrawable(getResources(), bitmap);
imageView.setImageDrawable(drawable);
I used with context
//Convert bitmap to drawable
Drawable drawable = new BitmapDrawable(context.getResources(), bitmap);
1) bitmap to Drawable :
Drawable mDrawable = new BitmapDrawable(getResources(), bitmap);
// mImageView.setDrawable(mDrawable);
2) drawable to Bitmap :
Bitmap mIcon = BitmapFactory.decodeResource(context.getResources(),R.drawable.icon_resource);
// mImageView.setImageBitmap(mIcon);
If you have a bitmap image and you want to use it in drawable, like
Bitmap contact_pic; //a picture to show in drawable
drawable = new BitmapDrawable(contact_pic);
Just do this:
private void setImg(ImageView mImageView, Bitmap bitmap) {
Drawable mDrawable = new BitmapDrawable(getResources(), bitmap);
mImageView.setDrawable(mDrawable);
}
here's another one:
Drawable drawable = RoundedBitmapDrawableFactory.create(context.getResources(), bitmap);
For Kotlin users:
Kotlin has a function for converting Bitmap to BitmapDrawable:
Bitmap.toDrawable(resources)
covert bit map to drawable in sketchware app using code
android.graphics.drawable.BitmapDrawable d = new android.graphics.drawable.BitmapDrawable(getResources(), bitmap);

Categories

Resources