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 androidsnippets.com/convert-bitmap-to-drawableZala Janaksinh– Zala Janaksinh2012-07-12 09:44:32 +00:00Commented Jul 12, 2012 at 9:44
-
3Contribution is a great way to say thanx... :) Contributions in terms of giving answers... :)Farhan– Farhan2012-10-02 08:40:18 +00:00Commented Oct 2, 2012 at 8:40
-
@Farhan k ...... .Abhi– Abhi2016-04-18 10:31:39 +00:00Commented Apr 18, 2016 at 10:31
11 Answers
Sounds like you want to use BitmapDrawable
From the documentation:
A
Drawablethat wraps a bitmap and can be tiled, stretched, or aligned. You can create aBitmapDrawablefrom a file path, an input stream, through XML inflation, or from aBitmapobject.
1 Comment
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
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
I used with context
//Convert bitmap to drawable
Drawable drawable = new BitmapDrawable(context.getResources(), bitmap);
1 Comment
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
Just do this:
private void setImg(ImageView mImageView, Bitmap bitmap) {
Drawable mDrawable = new BitmapDrawable(getResources(), bitmap);
mImageView.setDrawable(mDrawable);
}