jul
jul

Reputation: 37484

Set BitmapDrawable resource in constructor

How can I set a resource in a BitmapDrawable constructor? In the example below I want to set the resource to R.drawable.default_fb_pic, which is an ID, not a resource. How can I get the resource? With an ImageView we can set the resource with its ID:

imageview.setImageResource(R.drawable.whatever);  

I'm not very clear with why sometimes the resource can be set directly with its ID, and sometimes not.

Anybody can help?

static class DownloadedDrawable extends BitmapDrawable {            

    public DownloadedDrawable(BitmapDownloaderTask bitmapDownloaderTask) {
        super(R.drawable.default_fb_pic); //"The constructor BitmapDrawable(int)
        //is undefined",
        //which I understand because it takes a resource.
        //But how do I get the resource and why is there no constructor
        //directly with the id?
    }

    ...
}

Upvotes: 1

Views: 6228

Answers (2)

Javad Jafari
Javad Jafari

Reputation: 730

getResources().getDrawble(int id) was already depreciated in API 22. it is highly recommended to use contextCompat.getDrawble(Context ctx, int id) instead.

Upvotes: 3

Blundell
Blundell

Reputation: 76564

There is

Context.getResources()

http://developer.android.com/reference/android/content/Context.html#getResources()

And you can chain it with

getDrawable()

http://developer.android.com/reference/android/content/res/Resources.html#getDrawable(int)

mContext.getResources().getDrawable(R.drawable.default_fb_pic);

You just need a reference to your context.

Upvotes: 5

Related Questions