Reputation: 6275
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.
Upvotes: 14
Views: 21166
Reputation: 1402
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:
Drawable
to get out Bitmap on the screen.Drawable
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())
.
Upvotes: 0
Reputation: 2268
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
Upvotes: 3
Reputation: 47817
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
Upvotes: 3
Reputation: 6118
BitmapDrawable(Bitmap bitmap)
constructor has been deprecated
you can use
Drawable drawable = new BitmapDrawable(getResources(), bitmap);
Upvotes: 2
Reputation: 7114
you can use the BitmapDrawable class to get a Drawable from a bitmap
Bitmap Drawable reference
Upvotes: 0
Reputation: 133560
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
Upvotes: 26