How to convert a Bitmap to Drawable in android? - android

How can I convert a Bitmap image to Drawable ?

Try this it converts a Bitmap type image to Drawable
Drawable d = new BitmapDrawable(getResources(), bitmap);

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.

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.

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

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

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

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

Just do this:
private void setImg(ImageView mImageView, Bitmap bitmap) {
Drawable mDrawable = new BitmapDrawable(getResources(), bitmap);
mImageView.setDrawable(mDrawable);
}

here's another one:
Drawable drawable = RoundedBitmapDrawableFactory.create(context.getResources(), bitmap);

For Kotlin users:
Kotlin has a function for converting Bitmap to BitmapDrawable:
Bitmap.toDrawable(resources)

covert bit map to drawable in sketchware app using code
android.graphics.drawable.BitmapDrawable d = new android.graphics.drawable.BitmapDrawable(getResources(), bitmap);

Related

How to convert imageurl to drawable?

First of all i searched a lot and didn't find solution for my case, i need to convert the imageUrl to drawable, what i tried as far :
File imageFile = DiskCacheUtils.findInCache(c.profilePhoto, com.nostra13.universalimageloader.core.ImageLoader.getInstance().getDiscCache());
String file_target = "file://" + imageFile.getPath();
BitmapDrawable bitDraw = (BitmapDrawable) BitmapDrawable.createFromPath(file_target);
if (profilePhotos.size() == 4) break;
BitmapDrawable drawable = bitDraw; //getResources().getDrawable(c.profilePhoto);
drawable.setBounds(0, 0, width, height);
profilePhotos.add(drawable);
Also i tried to load the image into bitmap via Glide, but didn't work.
How i can achieve that ?
Thanks in advance.
first convert URL to Bitmap and pass bitmap into these function
Drawable d= new BitmapDrawable(context.getResources(), bitmap);
return d;

how to change canvas with Drawable?

I need to convert the rotated canvas image to drawable.
Conversely, there are many ways to convert from drawable to canvas, but I could not find what I wanted.
Is it not common to convert from Canvas to Drawable?
Should I use another method?
Please let me know, Thank you.
View v = new View(this);
v.setDrawingCacheEnabled(true);
v.draw(canvas);
Bitmap b = v.getDrawingCache();
Drawable drawable = new BitmapDrawable(getResources(), b);
You can create a bitmap from your canvas as described in this asnwer, hen you can generate your BitmapDrawable this way new BitmapDrawable(getResources(), yourBitmap)

How can i convert a statelistDrawable to bitmapdrawable in android

How can i convert a state list Drawable to bitmap drawable in android.
I want to convert a statelist drawable into bitmap drawable.
I found a work around if i find any icon whose type is other then bitmap drawable then use this code
here searchResult.getIcon() is my icon value
Bitmap bitmap = Bitmap.createBitmap(searchResult.getIcon().getIntrinsicWidth(),searchResult.getIcon().getIntrinsicHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
searchResult.getIcon().setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
searchResult.getIcon().draw(canvas);
itemIconBitmap = bitmap;
e.printStackTrace();
You can use toBitmap or casting the current drawable
(stateListDrawable as? StateListDrawable)?.let { drawable ->
(drawable.current as BitmapDrawable).bitmap
}

Create drawable from bitmap

ALL,
As suggested here I need to make a drawable out of my bitmap. But when I tried to use:
Drawable d = new Drawable( my_bmp);
it shows that this constructor is deprecated in favour of:
Drawable(Bitmap bmp, int resourceId)
How else I can make a drawable out of bitmap?
Thank you.
You can use
Drawable d = new BitmapDrawable(getResources(), my_bmp);
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.
BitmapDrawable(Resources res, Bitmap bitmap)
Create drawable from a bitmap, setting initial target density based on
the display metrics of the resources.
Also look a the public constructors #
http://developer.android.com/reference/android/graphics/drawable/BitmapDrawable.html
try this to load Bitmap as Bitmap Drawable
Create ImgDrawableFromFile(Resources res, String file_name) like below
Drawable d = null;
public Drawable ImgDrawableFromFile(Resources res, String file_name) {
myBitmap = null;
File imgFile = new File(
"/data/data/yourpkgname/app_my_sub_dir/images/" + file_name);
if (imgFile.exists()) {
myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
if (myBitmap != null) {
d = new BitmapDrawable(res, myBitmap);
return d;
} else {
return null;
}
}
return null;
}
Now called this function like
img.setBackgroundDrawable(ImgDrawableFromFile(getResources(), "1.jpg")); // pass your saved file name as second argument
A little late to the party but I see a clear misunderstanding. The answers provided are indeed correct:
Drawable d = new BitmapDrawable(getResources(),bitmap);
In this case getResources() is a method available in the Context or Activity and is only used to find out the screen density and adjust the Drawable accordingly.
So Igor, even if you are loading the image from the cloud, you can still call getResources().
Happy coding
BitmapDrawable(Bitmap bitmap) constructor has been deprecated
you can use
Drawable drawable = new BitmapDrawable(getResources(), bitmap);
source
you can use the BitmapDrawable class to get a Drawable from a bitmap
Bitmap Drawable reference
If Mauro Banze was late to the party then I don't really know what I'm still doing here. But for the ones that come here desperately looking for a solution, here is what I did:
I stumbled over somewhat the same issue when bringing a Project from JavaFX to Android. I wanted to reuse most of my "data" classes which only manage and provide my data and have no affect on the UI.
Enough story telling, here we go:
Problem:
At some point we need a Drawableto get out Bitmap on the screen.
We want to download (from the cloud, the internet, or wherever) this Bitmap and save it for later usage as a Drawable
We can't do that in a non-Context or non-Activity class, as we need the Resources.
So why don't we just save it as a Bitmap until we need it to be drawn (which certainly will happen in a Context or Activity class).
I have created a class called BitmapImage. It looks like this:
public class BitmapImage {
private final Bitmap bitmap;
public BitmapImage (Bitmap bitmap) {
this.bitmap = bitmap;
}
public Bitmap getBitmap() {
return bitmap;
}
public Drawable getDrawable(Resources res) {
return new BitmapDrawable(res,getBitmap());
}
This very simple class "saves" the Bitmap. Thus, rather then working with a Drawable you work with the BitmapImage until you really desperately need the Drawable.
At that point, you should be in your Activity or Context and there you can call Drawable foo = anyBitmapImage.getDrawable(getResource()).

How to get the whole bitmap attached to an ImageView?

I tried to get Bitmap attached to an ImageView, using ImageView.getDrawingCache(); But I found that the returned Bitmap was not the same as I'd like to get from the ImageView. It was always smaller than the real image.
I had known that, the method getDrawingCache() should not have the view if it is bigger than the screen as the visible portion of the view is only drawn and the cache holds only what is drawn.
Could I get the whole bitmap attached to a ImageView?
If you just want the Bitmap from a ImageView the following code may work for you:-
Bitmap bm=((BitmapDrawable)imageView.getDrawable()).getBitmap();
I think that's what you wanted.
If your drawble is not always an instanceof BitmapDrawable
Note: ImageView should be set before you do this.
Bitmap bitmap;
if (mImageView.getDrawable() instanceof BitmapDrawable) {
bitmap = ((BitmapDrawable) mImageView.getDrawable()).getBitmap();
} else {
Drawable d = mImageView.getDrawable();
bitmap = Bitmap.createBitmap(d.getIntrinsicWidth(), d.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
d.draw(canvas);
}
Your bitmap is stored in bitmap.
Voila!
Easiest way is to set tag in ImageView.
imageView.setImageBitmap(bitmap);
imageView.setTag(bitmap);
To get Tag from it
imageView.getTag();

Categories

Resources