How to convert a Bitmap to Drawable in android?
AndroidBitmapAndroid DrawableAndroid Problem Overview
How can I convert a Bitmap image to Drawable ?
Android Solutions
Solution 1 - Android
Try this it converts a Bitmap
type image to Drawable
Drawable d = new BitmapDrawable(getResources(), bitmap);
Solution 2 - Android
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.
Solution 3 - Android
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.
Solution 4 - Android
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);
Solution 5 - Android
I used with context
//Convert bitmap to drawable
Drawable drawable = new BitmapDrawable(context.getResources(), bitmap);
Solution 6 - Android
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);
Solution 7 - Android
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);
Solution 8 - Android
Just do this:
private void setImg(ImageView mImageView, Bitmap bitmap) {
Drawable mDrawable = new BitmapDrawable(getResources(), bitmap);
mImageView.setDrawable(mDrawable);
}
Solution 9 - Android
here's another one:
Drawable drawable = RoundedBitmapDrawableFactory.create(context.getResources(), bitmap);
Solution 10 - Android
covert bit map to drawable in sketchware app using code
android.graphics.drawable.BitmapDrawable d = new android.graphics.drawable.BitmapDrawable(getResources(), bitmap);
Solution 11 - Android
For Kotlin users:
Kotlin has a function for converting Bitmap
to BitmapDrawable
:
Bitmap.toDrawable(resources)