BitmapFactory.Options da 0 ancho y altura
final BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeFile(path, options); final int height = options.outHeight; final int width = options.outWidth;
Path es la ruta del archivo de imagen que es correcta.
El problema es options.outHeight y options.outWidth son 0 cuando la imagen se captura en modo horizontal con AutoRotate activado . Si apago AutoRotate , funciona bien. Dado que su ancho y altura son 0, estaba recibiendo un bitmap nulo al final.
- ¿Cómo cargar imágenes grandes en Android y evitar el error de memoria?
- Convertir Bitmap a Byte matriz SIN Bitmap.CompressFormat
- Obtener mapa de bits de zoom visible android imagen
- Explicación del método getPixels para un mapa de bits en Android
- ¿Hay alguna forma de obtener el Rect en el que Bitmap se dibuja en Android?
Código Completo:
Bitmap photo = decodeSampledBitmapFromFile(filePath, DESIRED_WIDTH, DESIRED_HEIGHT); public static Bitmap decodeSampledBitmapFromFile(String path, int reqWidth, int reqHeight) { // First decode with inJustDecodeBounds=true to check dimensions final BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeFile(path, options); // Calculate inSampleSize, Raw height and width of image final int height = options.outHeight; final int width = options.outWidth; options.inPreferredConfig = Bitmap.Config.ARGB_8888; int inSampleSize = 1; if (height > reqHeight) { inSampleSize = Math.round((float) height / (float) reqHeight); } int expectedWidth = width / inSampleSize; if (expectedWidth > reqWidth) { inSampleSize = Math.round((float) width / (float) reqWidth); } options.inSampleSize = inSampleSize; // Decode bitmap with inSampleSize set options.inJustDecodeBounds = false; return BitmapFactory.decodeFile(path, options); }
- Aumentar la memoria asignada a la aplicación
- Android: ¿Cómo renderizar una imagen de flujo continuo a ImageView sin cargar el mapa de bits completo?
- Cambiar el tamaño de la imagen del archivo
- Superposición de color en android NDK
- La imagen de la galería gira automáticamente - Android
- Android intentando usar un mapa de bits reciclado, no en mi código
- Cómo manejar try catch exception android
- Cómo crear mapa de bits de GlSurfaceView
Tuve el mismo problema, lo arreglé cambiando:
BitmapFactory.decodeFile(path, options);
a:
try { InputStream in = getContentResolver().openInputStream( Uri.parse(path)); BitmapFactory.decodeStream(in, null, options); } catch (FileNotFoundException e) { // do something }
Después de ese cambio, el ancho y la altura se ajustaron correctamente.