501

How can I convert a Bitmap image to Drawable ?

3
  • 3
    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 Commented Jul 12, 2012 at 9:44
  • 3
    Contribution is a great way to say thanx... :) Contributions in terms of giving answers... :) Commented Oct 2, 2012 at 8:40
  • @Farhan k ...... . Commented Apr 18, 2016 at 10:31

11 Answers 11

893

Try this it converts a Bitmap type image to Drawable

Drawable d = new BitmapDrawable(getResources(), bitmap);
Sign up to request clarification or add additional context in comments.

Comments

283

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.

1 Comment

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

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.

4 Comments

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.
35

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

2 Comments

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

I used with context

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

1 Comment

Duplicate answer.
24

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

Comments

20

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

4 Comments

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
11

Just do this:

private void setImg(ImageView mImageView, Bitmap bitmap) {

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

1 Comment

Not the solution for what he is asking
2

here's another one:

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

Comments

2

For Kotlin users:

Kotlin has a function for converting Bitmap to BitmapDrawable:

Bitmap.toDrawable(resources)

Comments

0

convert Bitmap To Drawable in Sketchware app using direct block

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.