How to get Bitmap from android's default resource?

It's because this resource is a StateListDrawable, not a Bitmap. You should use:

 Drawable d = getResources().getDrawable(android.R.drawable.list_selector_background);

If you're sure that it's a bitmap, cast the drawable to BitmapDrawable and get bitmap from there:

Drawable currentState = d.getCurrent();
if(currentState instanceof BitmapDrawable)
    Bitmap b = ((BitmapDrawable)currentState).getBitmap();

The bitmap may be null as well.


You should use Resources.getSystem() instead of getResources

Bitmap bm = BitmapFactory.decodeResource(Resources.getSystem(), android.R.drawable.list_selector_background);

Because the resource you are trying to access is not in your own resources, but in the system's.

Edit:

Next to the wrong resources, you are trying to get a drawable from an resource that is a selector (xml file). Please get the right drawable resource :) http://androidxref.com/4.1.1/xref/frameworks/base/core/res/res/drawable/list_selector_background.xml


Without using BitmapFactory:

Bitmap bmp = ((BitmapDrawable) context.getResources()
             .getDrawable(R.drawable.list_selector_background)).getBitmap();

Tags:

Android

Bitmap