BitmapFactory.decodeResource() renvoie la valeur null pour la forme définie en xml drawable
J'ai regardé à travers de multiples questions similaires, bien que je n'ai pas trouvé la bonne réponse à ma requête.
J'ai un drawable, défini dans 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>
Je veux le convertir en objet Bitmap afin d'effectuer certaines opérations, mais BitmapFactory.decodeResource()
renvoie la valeur null.
C'est comment je le fais:
Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.shape);
Ce que je fais mal? Est BitmapFactory.decodeResource()
applicable pour xml défini un drawable?
Vous devez vous connecter pour publier un commentaire.
Puisque vous voulez charger un
Drawable
, pas unBitmap
, utilisez ceci:De le transformer en un
Bitmap
:Prises à partir de: Comment faire pour convertir un Drawable à un Bitmap?
getIntrinsicWidth()
etgetIntrinsicHeight()
renvoie toujours -1 et le bitmap n'est pas créée. Ou suis-je quelque chose de mal?IllegalArgumentException: width and height must be > 0
c'est un drawable, pas une image bitmap. Vous devez utiliser
getDrawable
au lieugetDrawable
sur un StateListDrawable ou Forme