Obtención de mapa de bits de vector dibujable

En mi aplicación, tengo que configurar un icono grande para una notificación. LargeIcon debe ser un mapa de bits, y mis dibujables son imágenes vectoriales (la nueva característica de Android, vea este enlace ) El problema es cuando intento decodificar un recurso que es una imagen vectorial, obtengo un valor nulo devuelto.

Aquí está la muestra del código:

if (BitmapFactory.decodeResource(arg0.getResources(), R.drawable.vector_menu_objectifs) == null) Log.d("ISNULL", "NULL"); else Log.d("ISNULL", "NOT NULL"); 

En este ejemplo, cuando reemplazo R.drawable.vector_menu_objectifs con una imagen "normal", un png por ejemplo, el resultado no es null (obtengo el bitmap correcto) ¿Hay algo que me falta?

Verificado en la API: 17, 21, 23

 public static Bitmap getBitmapFromVectorDrawable(Context context, int drawableId) { Drawable drawable = ContextCompat.getDrawable(context, drawableId); if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { drawable = (DrawableCompat.wrap(drawable)).mutate(); } Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight()); drawable.draw(canvas); return bitmap; } 

ACTUALIZAR:

Proyecto gradle:

 dependencies { classpath 'com.android.tools.build:gradle:2.2.0-alpha5' } 

Módulo gradle:

 android { compileSdkVersion 23 buildToolsVersion '23.0.3' defaultConfig { minSdkVersion 16 targetSdkVersion 23 vectorDrawables.useSupportLibrary = true } ... } ... 

Puede utilizar el siguiente método:

 @TargetApi(Build.VERSION_CODES.LOLLIPOP) private static Bitmap getBitmap(VectorDrawable vectorDrawable) { Bitmap bitmap = Bitmap.createBitmap(vectorDrawable.getIntrinsicWidth(), vectorDrawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); vectorDrawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight()); vectorDrawable.draw(canvas); return bitmap; } 

Que a veces me combinan con:

 private static Bitmap getBitmap(Context context, int drawableId) { Drawable drawable = ContextCompat.getDrawable(context, drawableId); if (drawable instanceof BitmapDrawable) { return ((BitmapDrawable) drawable).getBitmap(); } else if (drawable instanceof VectorDrawable) { return getBitmap((VectorDrawable) drawable); } else { throw new IllegalArgumentException("unsupported drawable type"); } } 

Basado en las respuestas anteriores, puede simplificarse de este modo para que coincida con VectorDrawable y BitmapDrawable y sea compatible con al menos API 15.

 public static Bitmap getBitmapFromDrawable(Context context, @DrawableRes int drawableId) { Drawable drawable = ContextCompat.getDrawable(context, drawableId); if (drawable instanceof BitmapDrawable) { return ((BitmapDrawable) drawable).getBitmap(); } else if (drawable instanceof VectorDrawableCompat || drawable instanceof VectorDrawable) { Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight()); drawable.draw(canvas); return bitmap; } else { throw new IllegalArgumentException("unsupported drawable type"); } } 

Entonces usted tiene que agregar en su archivo gradle:

 android { defaultConfig { vectorDrawables.useSupportLibrary = true } } 

En pre-Lollipop utilizará VectorDrawableCompat y en Lollipop utilizará VectorDrawable.

EDITAR

He editado la condición después del comentario de @ user3109468

  • Sustitución de un color en un mapa de bits
  • Crear dibujable desde mapa de bits
  • Obtenga el ancho y la altura del mapa de bits sin cargar en la memoria
  • ¿Cómo puedo rotar un mapa de bits en Android?
  • Representación de mapa de bits de Eclipse en el depurador
  • ¿Cuándo reciclar mapa de bits en el proyecto android?
  • Android BitmapFactory.decodeFile intermitentemente devuelve null
  • Cambiar el color del mapa de bits
  • Obtener datos Exif usando Uri
  • El mapa de bits guardado en android tiene 3 bytes de largo cuando se lee
  • Mapa de bits de mosaico de Android
  • FlipAndroid es un fan de Google para Android, Todo sobre Android Phones, Android Wear, Android Dev y Aplicaciones para Android Aplicaciones.