Android Save Image Exif Information goes Wrong in Marshmallow 6.0.1-ThrowExceptions

Exception or error:

In my custom camera, I need to save the orientation for a captured image. This code works perfectly for other android versions. But its not working in 6.0.1. The result which am getting is wrong after saving the attributes to image file.

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

Try this for saving the orientation of different angles for captured Images :-

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) {

    }

###

The other solutions are rewriting the image instead of manipulating the EXIF information. I would suggest to do it like you tried just with the correct constants:

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

Based on the sourcecode you need to use one of these values:

  • 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

###

Since I have experienced problems and different behaviour reading EXIF information from different manufacturer I’d suggest that you get the orientation from the saved image URI, then you could save it to the EXIF interface.

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);

}

###

SUPPORT LIBRARY UPDATE:

Google yesterday released 25.1.0 version of the support library with a massive update in the ExifInterface which mainly focuses on reading and writing of attributes of an Image file on the latest android version. Please have a look to the SOURCE for code and more understanding this update.

Hope this helps you

Leave a Reply

Your email address will not be published. Required fields are marked *