Android Guardar imagen Exif Información goes Wrong in Marshmallow 6.0.1

En mi cámara personalizada, necesito guardar la orientación de una imagen capturada. Este código funciona perfectamente para otras versiones de Android. Pero no funciona en 6.0.1. El resultado que obtengo es incorrecto después de guardar los atributos en el archivo de imagen.

try { exif = new ExifInterface(pictureFile.getAbsolutePath()); exif.setAttribute(ExifInterface.TAG_ORIENTATION, "" + orientation); exif.saveAttributes(); } catch (IOException e) { e.printStackTrace(); } 

Pruebe esto para guardar la orientación de los diferentes ángulos de las imágenes capturadas: –

 Options options = new Options(); // downsizing image as it throws OutOfMemory Exception for larger // images options.inSampleSize = 8; ExifInterface exif; try { exif = new ExifInterface(fileUri.getPath()); int orientation = exif.getAttributeInt( ExifInterface.TAG_ORIENTATION, 0); Log.d("EXIF", "Exif: " + orientation); Matrix matrix = new Matrix(); if (orientation == 6) { matrix.postRotate(90); Log.d("EXIF", "Exif: " + orientation); } else if (orientation == 3) { matrix.postRotate(180); Log.d("EXIF", "Exif: " + orientation); } else if (orientation == 8) { matrix.postRotate(270); Log.d("EXIF", "Exif: " + orientation); } myBitmap = BitmapFactory.decodeFile(path_img, options); myBitmap = Bitmap.createBitmap(myBitmap, 0, 0, myBitmap.getWidth(), myBitmap.getHeight(), matrix, true); } catch (Exception e) { } 

Las otras soluciones están reescribiendo la imagen en lugar de manipular la información EXIF. Yo sugeriría hacerlo como lo intentó con las constantes correctas:

 try { exif = new ExifInterface(pictureFile.getAbsolutePath()); exif.setAttribute(ExifInterface.TAG_ORIENTATION, Integer.toString(ExifInterface.ORIENTATION_ROTATE_90)); exif.saveAttributes(); } catch (IOException e) { e.printStackTrace(); } 

Basado en el código fuente necesitas usar uno de estos valores:

  • ExifInterface.ORIENTATION_UNDEFINED
  • ExifInterface.ORIENTATION_NORMAL
  • ExifInterface.ORIENTATION_FLIP_HORIZONTAL
  • ExifInterface.ORIENTATION_ROTATE_180
  • ExifInterface.ORIENTATION_FLIP_VERTICAL
  • ExifInterface.ORIENTATION_TRANSPOSE
  • ExifInterface.ORIENTATION_ROTATE_90
  • ExifInterface.ORIENTATION_TRANSVERSE
  • ExifInterface.ORIENTATION_ROTATE_270

Puesto que he experimentado problemas y el comportamiento diferente que leía la información de EXIF ​​de diverso fabricante sugeriría que usted consiga la orientación de la imagen ahorrada URI, después usted podría ahorrarla a la interfaz de EXIF.

 public static void getImageOrientationFromUri(@NonNull ContentResolver contentResolver, @NonNull Uri uri) { if (uri.getPath() == null) throw new NullPointerException("URI Path should not be null"); float rotationInDegrees = 0; Cursor cursor = contentResolver.query(uri, new String[]{MediaStore.Images.ImageColumns.ORIENTATION}, null, null, null); if (cursor != null && cursor.moveToFirst()) { int col = cursor.getColumnIndex(MediaStore.Images.ImageColumns.ORIENTATION); if (col != -1) rotationInDegrees = cursor.getInt(col); cursor.close(); } // here you can save to the EXIF interface getting the apropriate value from rotationInDegrees //If you want to display the image create the bitmap using: //Bitmap sourceBitmap = MediaStore.Images.Media.getBitmap(contentResolver, uri); //Matrix matrix = new Matrix(); //matrix.preRotate(rotationInDegrees); //you can change the signature of the method to return a `Bitmap` and return //Bitmap.createBitmap(sourceBitmap, 0, 0, sourceBitmap.getWidth(), sourceBitmap.getHeight(), matrix, false); } 

ACTUALIZACIÓN DE LA BIBLIOTECA DE APOYO:

Google lanzó ayer la versión 25.1.0 de la biblioteca de soporte con una actualización masiva en el ExifInterface que se centra principalmente en la lectura y escritura de los atributos de un archivo de imagen en la última versión android. Por favor, eche un vistazo a la fuente de código y más comprensión de esta actualización.

Espero que esto te ayude

  • ¿Evita que la orientación de Live Wallpaper cambie cuando se abre una aplicación y se cambia la orientación?
  • Orientación de la pantalla y valores en manifest.xml
  • Cómo utilizar ExifInterface con una secuencia o un URI
  • Android obtener el tamaño de la pantalla de la orientación de la pantalla de otros
  • Cambiar la orientación del dispositivo Android desde la línea de comandos
  • Datos EXIF ​​de Android siempre 0, cómo cambiarlo?
  • Diferencia entre SCREEN_ORIENTATION_USER y SCREEN_ORIENTATION_SENSOR
  • Mostrar imágenes desde el tamaño de la URL y los problemas de orientación de la pantalla
  • Cómo obtener los ángulos euler del vector de rotación (Sensor.TYPE_ROTATION_VECTOR)
  • Problema con la orientación de la imagen de Android en Motorola Droid
  • ¿Cuál es la diferencia entre Display.getRotation () y Display.getOrientation () aparte del hecho de que Display.getOrientation () está obsoleto?
  • FlipAndroid es un fan de Google para Android, Todo sobre Android Phones, Android Wear, Android Dev y Aplicaciones para Android Aplicaciones.