How can I convert a Bitmap image to Drawable ?
-
2Hi 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 – Zala Janaksinh Jul 12 '12 at 9:44
-
2Contribution is a great way to say thanx... :) Contributions in terms of giving answers... :) – Farhan Oct 2 '12 at 8:40
-
@Farhan k ...... . – Abhi Apr 18 '16 at 10:31
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.
-
19@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
-
71non-useful post, non-useful comment. would be better if you add some code – voghDev Aug 18 '14 at 9:31
Try this it converts a Bitmap
type image to Drawable
Drawable d = new BitmapDrawable(getResources(), bitmap);
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.
-
9At 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 3 '12 at 16:27
-
3this is a more accurate answer considering the one from @Manoj is deprecated. – Raykud Mar 9 '12 at 21:51
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);
-
1
-
-
-
3
I used with context
//Convert bitmap to drawable
Drawable drawable = new BitmapDrawable(context.getResources(), bitmap);
-
3
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);
-
20That'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 9 '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 9 '14 at 11:55
-
@schlingel yes, but still some one in rush use this and it make a work – Pir Fahim Shah Jul 9 '14 at 15:03
Just do this:
private void setImg(ImageView mImageView, Bitmap bitmap) {
Drawable mDrawable = new BitmapDrawable(getResources(), bitmap);
mImageView.setDrawable(mDrawable);
}
-
1
here's another one:
Drawable drawable = RoundedBitmapDrawableFactory.create(context.getResources(), bitmap);