14

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.

6
  • can you try this Drawable d=new BitmapDrawable(res, bmp); ? Commented Apr 15, 2014 at 4:58
  • @SimplePlan, as I wrote in the original thread, bitmap comes from the cloud, not resource. Commented Apr 15, 2014 at 5:26
  • then you have to download that image from cloud and then load as Drawable. Commented Apr 15, 2014 at 5:34
  • @SimplePlan, yes, the image is loaded as Bitmap. The problem is - convert to Drawable. Can you give some code? Commented Apr 15, 2014 at 5:47
  • ok wait ill post my answer. Commented Apr 15, 2014 at 5:48

6 Answers 6

26

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

Sign up to request clarification or add additional context in comments.

5 Comments

you answer tells me you didn't look at the original post. ;-) I am not loading my bitmap from the resources. I am getting the bitmap as the photo from the cloud. Therefore I cannot use resource references in that code. Any other idea?
@Igor you never mentioned that in your post. its not my fault.
I did however, referenced a link to my original question where this requirement was mentioned. Like I said - you didn't look. ;-)
@Igor you could have mentioned that in your post. you din't also posting links to external source is not good as not many will look a the same :)
@Igor did you even check at the link i posted?? first download the bitmap then use BitmapDrawable(Resources res, String filepath) filepath is the path of the bitmap
3

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 

Comments

3

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

Comments

2

BitmapDrawable(Bitmap bitmap) constructor has been deprecated

you can use

Drawable drawable = new BitmapDrawable(getResources(), bitmap);

source

1 Comment

@Igor Get the bitmpa from the cloud: Bitmap bitmap = BitmapFactory.decodeStream(new URL(url).openConnection().getInputStream());
0

you can use the BitmapDrawable class to get a Drawable from a bitmap

Bitmap Drawable reference

1 Comment

Well, I can't. I am not using resources, bitmap (photo) comes from the cloud.
0

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()).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.