I looked through multiple similar questions, although I haven't found proper answer on my query.
I have a drawable, defined in shape.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
<solid android:color="@color/bg_color" />
</shape>
I want to convert it to Bitmap object in order to perform some operations, but BitmapFactory.decodeResource()
returns null.
This is how I'm doing it:
Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.shape);
What am I doing wrong? Is BitmapFactory.decodeResource()
applicable for xml defined drawables?
Since you want to load a
Drawable
, not aBitmap
, use this:To turn it into a
Bitmap
:Taken from: How to convert a Drawable to a Bitmap?
You may have put
.xml
into directory:.../drawable-24
, and try to put it into .../drawable
instead.it works for me, hope this can help someone.
it is a drawable, not a bitmap. You should use
getDrawable
instead