Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Save Image Exif Information goes Wrong in Marshmallow 6.0.1

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();
}
like image 834
Ajay Venugopal Avatar asked Dec 01 '16 12:12

Ajay Venugopal


2 Answers

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

    }
like image 170
Ankit Gupta Avatar answered Oct 26 '22 07:10

Ankit Gupta


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
like image 38
rekire Avatar answered Oct 26 '22 06:10

rekire