ChatGPT解决这个技术问题 Extra ChatGPT

How to convert a Bitmap to Drawable in android?

How can I convert a Bitmap image to Drawable ?

Hi i got the answer of your question follow this link and got the right answer i do it. and i success,i hope you got the success. best of luck androidsnippets.com/convert-bitmap-to-drawable
Contribution is a great way to say thanx... :) Contributions in terms of giving answers... :)
@Farhan k ...... .

V
ViliusK

Try this it converts a Bitmap type image to Drawable

Drawable d = new BitmapDrawable(getResources(), bitmap);

This is what I expected!
Y
Yasin Kaçmaz

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.


@Deprecated Use BitmapDrawable(Resources, Bitmap) to ensure that the drawable has correctly set its target density.
V
ViliusK

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.


At least comment why if you're going to down vote. This is a perfectly valid answer, and brings extra information to solve issues that can occur with the other answers offered. Drawables made directly from a bitmap often have scaling errors without the getResources() reference.
this is a more accurate answer considering the one from @Manoj is deprecated.
@Raykud, Manoj's answer and this answer show the same code. And, how it is deprecated? And, how is this accurate if that is deprecated and this too is deprecated(It in real is not deprecated)
@Sambhav.K because original answer from Manoj was "Drawable d =new BitmapDrawable(bitmap);" but was edited years later.
Z
Ziem

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);

sorry...i was not serious
You could have upvoted instead of writing the same answer.
S
Samuel Ivan

I used with context

//Convert bitmap to drawable
Drawable drawable = new BitmapDrawable(context.getResources(), bitmap);

Duplicate answer.
Z
Ziem

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); 

That's deprecated now. Use the BitmapDrawable(Resources, Bitmap) constructor now.
@schlingel It still working fine and many of us are using it in our projects,
That's good for you, but doesn't help when Google eventually kills this constructor and you have to rewrite everything.
@schlingel yes, but still some one in rush use this and it make a work
S
Sanjayrajsinh

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);

T
Thiago

Just do this:

private void setImg(ImageView mImageView, Bitmap bitmap) {

    Drawable mDrawable = new BitmapDrawable(getResources(), bitmap);
    mImageView.setDrawable(mDrawable);
}

Not the solution for what he is asking
D
Desolator

here's another one:

Drawable drawable = RoundedBitmapDrawableFactory.create(context.getResources(), bitmap);

a
amosu dona

covert bit map to drawable in sketchware app using code

    android.graphics.drawable.BitmapDrawable d = new android.graphics.drawable.BitmapDrawable(getResources(), bitmap);

f
fif.iva

For Kotlin users:

Kotlin has a function for converting Bitmap to BitmapDrawable:

Bitmap.toDrawable(resources)