19

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();
}
rekire
  • 47,260
  • 30
  • 167
  • 264
Ajay Venugopal
  • 1,544
  • 1
  • 17
  • 30
  • What gets saved and how do you get the orientation? – Rene M. Dec 01 '16 at 12:13
  • i took the orientation using SensorEventListener – Ajay Venugopal Dec 01 '16 at 12:30
  • 1
    Then show us hwow. This is only a half question. When we didn't get the hole story. – Rene M. Dec 01 '16 at 12:44
  • Where is `pictureFile` coming from? – CommonsWare Dec 01 '16 at 12:47
  • i have a fragment with custom camera, with event listener for getting the orientation. when i click capture button i will save the image without any orientation and after that i will save the exif information based on the angle which i got through the sensor listener. It will show portrait image even when we took in any direction like landscape left , landscape down , or even up side down . the thing is this orientation is not working in 6.0.1. The exif information is saving wrong .. thats it please help me out – Ajay Venugopal Dec 01 '16 at 17:09
  • 1
    I think you are missing run-time permission for storage.(Required for Android 6.0 and higher). – Pravin Divraniya Dec 13 '16 at 09:33
  • Nop thats all good @PravinD – Ajay Venugopal Dec 13 '16 at 10:41
  • Might be device specific issue, In which device you are getting this issue? – Pravin Divraniya Dec 13 '16 at 10:55
  • am getting the issues on most of the 6.0.1 devices excluding MI phones – Ajay Venugopal Dec 13 '16 at 11:35
  • Have you tried some different approach(3'rd party library or something) as per my knowledge you just require Image in its proper orientation while displaying/saving. – Pravin Divraniya Dec 14 '16 at 14:45
  • yeah currently no 3rd partly library available to save the exif information – Ajay Venugopal Dec 15 '16 at 05:51
  • I might not be an expert on this, but have you tried the docs? https://developer.android.com/guide/topics/sensors/sensors_position.html#sensors-pos-orient This docs tells you how to get the device's orientation. Can combine with this answer as well: http://stackoverflow.com/questions/25972519/android-get-device-orientation-from-azimuth-roll-pitch – nic Dec 19 '16 at 17:09
  • @NicholasLie my problem is to saving the exif information of images not about getting orientation – Ajay Venugopal Dec 20 '16 at 05:58
  • 1
    Have you tried this library? https://github.com/sephiroth74/Android-Exif-Extended – Pravin Divraniya Dec 20 '16 at 13:08

4 Answers4

1

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

    }
Ankit Gupta
  • 64
  • 10
1

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

}
Ognian Gloushkov
  • 2,669
  • 1
  • 20
  • 35
1

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
rekire
  • 47,260
  • 30
  • 167
  • 264
0

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

Naveen Rao
  • 712
  • 6
  • 10