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:
- Sqlite Database - ¿Cómo guardar la imagen tomada de la intención de la cámara, así como hashmap a sqlite para recuperar más tarde?
- Decodificación de la imagen SVG a mapa de bits
- Cómo teñir un mapa de bits de Android en blanco?
- Cómo ver realmente un mapa de bits tomado de un volcado de montón de Android
- Cómo manejar Loading Images in Android
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?
- ¿"Bitmap.createScaledBitmap" convierte una imagen de 32 bits en 24 bits?
- Cambiar el tamaño del mapa de bits de InputStream
- Intención de inicio Intent.ACTION_VIEW no funciona en el archivo de imagen guardado
- OutofMemoryError: el tamaño de mapa de bits supera el presupuesto de VM (Android)
- Biblioteca de edición de imágenes para Android
- Guardando una imagen grande en PNG
- OutOfMemoryError mientras decodifica y codifica Base64 String en Bitmap
- ANR en Canvas.native_drawBitmap
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
- Barra de progreso de Android WebView
- ¿Cómo acceder a localhost desde un emulador Genymotion android?