494

How can I convert a Bitmap image to Drawable ?

Ali Khaki
  • 1,184
  • 1
  • 13
  • 24
Farha Ansari
  • 5,855
  • 5
  • 26
  • 22
  • 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 http://www.androidsnippets.com/convert-bitmap-to-drawable – Zala Janaksinh Jul 12 '12 at 09:44
  • 3
    Contribution 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 Answers11

887

Try this it converts a Bitmap type image to Drawable

Drawable d = new BitmapDrawable(getResources(), bitmap);
ViliusK
  • 11,345
  • 4
  • 67
  • 71
Manoj
  • 8,887
  • 2
  • 14
  • 2
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.

Yasin Kaçmaz
  • 6,573
  • 5
  • 40
  • 58
Graeme Duncan
  • 8,166
  • 2
  • 20
  • 13
162

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.

ViliusK
  • 11,345
  • 4
  • 67
  • 71
Zulaxia
  • 2,702
  • 2
  • 22
  • 23
  • 17
    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. – Zulaxia Mar 03 '12 at 16:27
  • 5
    this 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
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);
Ziem
  • 6,579
  • 8
  • 53
  • 86
Cristiana Chavez
  • 11,349
  • 5
  • 55
  • 54
32

I used with context

//Convert bitmap to drawable
Drawable drawable = new BitmapDrawable(context.getResources(), bitmap);
Samuel Seda
  • 2,814
  • 1
  • 24
  • 13
23

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);
Sanjayrajsinh
  • 15,014
  • 7
  • 73
  • 78
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); 
Ziem
  • 6,579
  • 8
  • 53
  • 86
Pir Fahim Shah
  • 10,505
  • 1
  • 82
  • 81
11

Just do this:

private void setImg(ImageView mImageView, Bitmap bitmap) {

    Drawable mDrawable = new BitmapDrawable(getResources(), bitmap);
    mImageView.setDrawable(mDrawable);
}
Thiago
  • 12,778
  • 14
  • 93
  • 110
2

here's another one:

Drawable drawable = RoundedBitmapDrawableFactory.create(context.getResources(), bitmap);
Desolator
  • 22,411
  • 20
  • 73
  • 96
2

For Kotlin users:

Kotlin has a function for converting Bitmap to BitmapDrawable:

Bitmap.toDrawable(resources)

fif.iva
  • 171
  • 2
  • 8
0

convert Bitmap To Drawable in Sketchware app using direct block

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