How can I convert a Bitmap image to Drawable ?
-
3Hi 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 http://www.androidsnippets.com/convert-bitmap-to-drawable – Zala Janaksinh Jul 12 '12 at 09:44
-
3Contribution is a great way to say thanx... :) Contributions in terms of giving answers... :) – Farhan Oct 02 '12 at 08:40
-
@Farhan k ...... . – Abhi Apr 18 '16 at 10:31
11 Answers
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 aBitmapDrawable
from a file path, an input stream, through XML inflation, or from aBitmap
object.

- 6,573
- 5
- 40
- 58

- 8,166
- 2
- 20
- 13
-
24@Deprecated Use BitmapDrawable(Resources, Bitmap) to ensure that the drawable has correctly set its target density. – Camille Sévigny Jun 24 '14 at 18:23
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.
-
17At 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. – Zulaxia Mar 03 '12 at 16:27
-
5this is a more accurate answer considering the one from @Manoj is deprecated. – Raykud Mar 09 '12 at 21:51
-
@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 Khandelwal Apr 21 '22 at 09:41
-
1@Sambhav.K because original answer from Manoj was "Drawable d =new BitmapDrawable(bitmap);" but was edited years later. – Raykud Apr 21 '22 at 16:40
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);

- 6,579
- 8
- 53
- 86

- 11,349
- 5
- 55
- 54
I used with context
//Convert bitmap to drawable
Drawable drawable = new BitmapDrawable(context.getResources(), bitmap);

- 2,814
- 1
- 24
- 13
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);

- 15,014
- 7
- 73
- 78
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);

- 6,579
- 8
- 53
- 86

- 10,505
- 1
- 82
- 81
-
21That's deprecated now. Use the BitmapDrawable(Resources, Bitmap) constructor now. – schlingel Jan 16 '13 at 11:55
-
1@schlingel It still working fine and many of us are using it in our projects, – Pir Fahim Shah Jul 09 '14 at 11:50
-
2That's good for you, but doesn't help when Google eventually kills this constructor and you have to rewrite everything. – schlingel Jul 09 '14 at 11:55
-
@schlingel yes, but still some one in rush use this and it make a work – Pir Fahim Shah Jul 09 '14 at 15:03
Just do this:
private void setImg(ImageView mImageView, Bitmap bitmap) {
Drawable mDrawable = new BitmapDrawable(getResources(), bitmap);
mImageView.setDrawable(mDrawable);
}

- 12,778
- 14
- 93
- 110
here's another one:
Drawable drawable = RoundedBitmapDrawableFactory.create(context.getResources(), bitmap);

- 22,411
- 20
- 73
- 96
For Kotlin users:
Kotlin has a function for converting Bitmap
to BitmapDrawable
:
Bitmap.toDrawable(resources)

- 171
- 2
- 8
convert Bitmap To Drawable in Sketchware app using direct block
android.graphics.drawable.BitmapDrawable d = new android.graphics.drawable.BitmapDrawable(getResources(), bitmap);

- 1
- 2