Android BitmapFactory.decodeResource() 为 xml drawable 中定义的形状返回 null

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24389043/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 08:04:34  来源:igfitidea点击:

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

androidandroid-drawablexml-drawable

提问by mol

I looked through multiple similar questions, although I haven't found proper answer on my query.

我浏览了多个类似的问题,但我的查询没有找到正确的答案。

I have a drawable, defined in shape.xml

我有一个 drawable,在 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.

我想将其转换为 Bitmap 对象以执行某些操作,但BitmapFactory.decodeResource()返回 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?

我究竟做错了什么?是BitmapFactory.decodeResource()适用于XML定义可绘制?

回答by Philipp Jahoda

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

由于您要加载 a Drawable,而不是 a Bitmap,请使用以下命令:

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

To turn it into a Bitmap:

把它变成一个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?

摘自:如何将 Drawable 转换为位图?

回答by Ali Bagheri

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);

回答by Raindeavor

You may have put .xmlinto directory:.../drawable-24, and try to put it into .../drawableinstead.

您可能已放入.xmldirectory:... /drawable-24,并尝试将其放入 ... /drawable

it works for me, hope this can help someone.

它对我有用,希望这可以帮助某人。

回答by Blackbelt

it is a drawable, not a bitmap. You should use getDrawableinstead

它是可绘制的,而不是位图。你应该getDrawable改用