BitmapFactory.decodeResource() returns null for shape defined in xml drawable
Asked Answered
M

5

57

I looked through multiple similar questions, although I haven't found a proper answer to my issue.

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?

Misfortune answered 24/6, 2014 at 14:19 Comment(0)
A
109

Since you want to load a Drawable, not a Bitmap, use this:

Drawable d = getResources().getDrawable(R.drawable.your_drawable, your_app_theme);

To turn it into a Bitmap:

public static Bitmap drawableToBitmap (Drawable drawable) {

    if (drawable instanceof BitmapDrawable) {
        return ((BitmapDrawable)drawable).getBitmap();
    }

    Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap); 
    drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
    drawable.draw(canvas);

    return bitmap;
}

Taken from: How to convert a Drawable to a Bitmap?

Algebra answered 24/6, 2014 at 14:21 Comment(5)
I wonder how this can work... Since the drawable is a shape defined in XML, getIntrinsicWidth() and getIntrinsicHeight() will always return -1 and the bitmap won't be created. Or am I getting something wrong?Piscary
This throws IllegalArgumentException: width and height must be > 0Byandby
That means that your drawable does not have a valid size.Algebra
Update with the new support library: use ContextCompat.GetDrawable(context, id)Burberry
Koltin extension for Drawable class: https://gist.github.com/gowthamgts/9d496f42ce0acd16194641f69fcc48a6Lamartine
I
9

Android KTX now has an extension function for converting drawable to bitmap

val bitmap = ContextCompat.getDrawable(context, R.drawable.ic_user_location_pin)?.toBitmap()

if (bitmap != null) {
    markerOptions.icon(BitmapDescriptorFactory.fromBitmap(bitmap))
}
Intrados answered 19/9, 2021 at 22:50 Comment(0)
T
2
public static Bitmap convertDrawableResToBitmap(@DrawableRes int drawableId, Integer width, Integer height) {
    Drawable d = getResources().getDrawable(drawableId);

    if (d instanceof BitmapDrawable) {
        return ((BitmapDrawable) d).getBitmap();
    }

    if (d instanceof GradientDrawable) {
        GradientDrawable g = (GradientDrawable) d;

        int w = d.getIntrinsicWidth() > 0 ? d.getIntrinsicWidth() : width;
        int h = d.getIntrinsicHeight() > 0 ? d.getIntrinsicHeight() : height;

        Bitmap bitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        g.setBounds(0, 0, w, h);
        g.setStroke(1, Color.BLACK);
        g.setFilterBitmap(true);
        g.draw(canvas);
        return bitmap;
    }

    Bitmap bit = BitmapFactory.decodeResource(getResources(), drawableId);
    return bit.copy(Bitmap.Config.ARGB_8888, true);
}

//------------------------

Bitmap b = convertDrawableResToBitmap(R.drawable.myDraw , 50, 50);
Thermodynamic answered 28/2, 2017 at 14:8 Comment(1)
add some explanation to your answerHumberto
S
0

it is a drawable, not a bitmap. You should use getDrawable instead

Squarrose answered 24/6, 2014 at 14:21 Comment(1)
You cannot use getDrawable on a StateListDrawable or ShapeByandby
H
0

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.

Hoop answered 24/7, 2019 at 2:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.