30

I have an app that captures photos using the native Camera and then uploads them to a server. My problem is that all the photos have an EXIF orientation value of 0, and this messes up the display elsewhere.

How can I change the EXIF orientation? I'm not looking for a way to correct it for every circumstance, just change it to a different value.

I'm using a Samsung Galaxy Note 4

I tried this solution that sets the camera orientation before taking photos: Setting Android Photo EXIF Orientation

Camera c = Camera.open();
c.setDisplayOrientation(90);

Camera.Parameters params = mCamera.getParameters();
params.setRotation(0); // tried 0, 90, 180
c.setParameters(params);

but it doesn't influence the resulting EXIF data, its still always 0

I also tried these solutions where the image is rotated after it is taken: EXIF orientation tag value always 0 for image taken with portrait camera app android

and while this rotates the photo, the EXIF orientation is still always 0.

I also tried setting the EXIF data directly: How to save Exif data after bitmap compression in Android

private Camera.PictureCallback mPicture = new Camera.PictureCallback() {
    @Override
    public void onPictureTaken(byte[] data, Camera camera) {
        final File pictureFile = getOutputMediaFile(MEDIA_TYPE_IMAGE, "");
        try {
            FileOutputStream fos = new FileOutputStream(pictureFile);

            ExifInterface exif = new ExifInterface(pictureFile.toString());
            exif.setAttribute(ExifInterface.TAG_ORIENTATION, "3");
            exif.saveAttributes();

            fos.write(data);
            fos.close();

            //upload photo..
        }
    }
}

but EXIF Orientation is still 0 after uploading.

I have also looked at these solutions:

Exif data TAG_ORIENTATION always 0

How to write exif data to image in Android?

How to get the Correct orientation of the image selected from the Default Image gallery

how to set camera Image orientation?

but they all involve correcting the orientation by rotating, which doesn't influence the EXIF data, or setting the EXIF data directly which doesn't seem to work.

How can I change the file's EXIF orientation data from 0 to 3?

UPDATE:

here is my upload code:

Bitmap sBitmap = null;
final File sResizedFile = getOutputMediaFile(MEDIA_TYPE_IMAGE, "_2");
try {
    sBitmap = BitmapFactory.decodeStream(new FileInputStream(pictureFile), null, options);
} catch (FileNotFoundException e) {
    Log.e("App", "[MainActivity] unable to convert pictureFile to bitmap");
    e.printStackTrace();
    return;
}

// ... compute sw and sh int values

Bitmap sOut = Bitmap.createScaledBitmap(sBitmap, sw, sh, false);
Bitmap rotatedBitmap = rotateBitmap(sOut, 3);
FileOutputStream sfOut;
try {
    sfOut = new FileOutputStream(sResizedFile);
    rotatedBitmap.compress(Bitmap.CompressFormat.JPEG, 70, sfOut);
    sfOut.flush();
    sfOut.close();
    sBitmap.recycle();
    sOut.recycle();
    rotatedBitmap.recycle();
} catch (Exception e) {
    Log.e("App", "[MainActivity] unable to save thumbnail");
    e.printStackTrace();
    return;
}
// upload small thumbnail
TransferObserver sObserver = transferUtility.upload(
            "stills/small",        /* The bucket to upload to */
            filename + ".jpg",     /* The key for the uploaded object */
            sResizedFile           /* The file where the data to upload exists */
);
4
  • on which device you are trying ?
    – MKJParekh
    Sep 13, 2016 at 7:32
  • Samsung Galaxy Note 4
    – Cbas
    Sep 13, 2016 at 17:29
  • @Cbas, is the "EXIF orientation value of 0" before or after you upload to online bucket? I just tested Amazon's AWS buckets and they retain EXIF after upload. You must be using a different bucket-based server (eg: Google Cloud), right? After your first code snippet you say "it doesn't influence the resulting EXIF data, its still always 0" so it's a code issue? Then after your second code snippet you say "but EXIF Orientation is still 0 after uploading." so it's an upload/server issue? Please confirm if EXIF value always 0 even before upload or only after upload?
    – VC.One
    Sep 18, 2016 at 23:21
  • @VC.One its 0 before the upload, definitely a code issue
    – Cbas
    Sep 19, 2016 at 0:01

5 Answers 5

30

As you can see, the The EXIF information is not reliable on Android (especially Samsung devices).

However the phone SQL database holding the references to Media object is reliable. I would propose going this way.

Getting the orientation from the Uri:

private static int getOrientation(Context context, Uri photoUri) {
    Cursor cursor = context.getContentResolver().query(photoUri,
            new String[]{MediaStore.Images.ImageColumns.ORIENTATION}, null, null, null);

    if (cursor.getCount() != 1) {
        cursor.close();
        return -1;
    }

    cursor.moveToFirst();
    int orientation = cursor.getInt(0);
    cursor.close();
    cursor = null;
    return orientation;
}

Then initialize rotated Bitmap:

public static Bitmap rotateBitmap(Context context, Uri photoUri, Bitmap bitmap) {
    int orientation = getOrientation(context, photoUri);
    if (orientation <= 0) {
        return bitmap;
    }
    Matrix matrix = new Matrix();
    matrix.postRotate(orientation);
    bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, false);
    return bitmap;
}

If you want to change the orientation of the image, try the following snippet:

public static boolean setOrientation(Context context, Uri fileUri, int orientation) {
    ContentValues values = new ContentValues();
    values.put(MediaStore.Images.Media.ORIENTATION, orientation);
    int rowsUpdated = context.getContentResolver().update(fileUri, values, null, null);
    return rowsUpdated > 0;
}

If you set the orientation of the image, later it will be constantly set at the correct orientation. There is need to make use of ExifInterface later, because the image is already rotated in proper way.

If this method is not satisfactory, then you could try this method

6
  • 1
    Yes, you are correct we should get the orientation of Uri data picked from gallery. Thanks Your solution worked for me!!!
    – Yyy
    Jul 12, 2017 at 6:58
  • 1
    You definitely deserved more than 10 votes for this answer. Thank you Jan 4, 2018 at 8:57
  • 1
    Works like a charm. Thank you!
    – Jorge181
    Jan 13, 2018 at 16:41
  • 4
    java.lang.NullPointerException: Attempt to invoke interface method 'int android.database.Cursor.getCount()' on a null object reference unfortunately
    – jujka
    Jun 6, 2019 at 14:04
  • @jujka high chance your file is not in the MediaStore images table. Insert it before querying for orientation value, like this. Aug 31, 2023 at 1:53
12
+300

You have accepted your own answer as solution. My rant is just useful side-info, anyways...

The "However..." in your Answer suggests while you now know the cause, you don't have a fix.

Turns out my code was able to set the EXIF data, but there is a discrepancy between how Android interprets this data and how iOS... interprets it.

This could be an endianness issue. You can try manually changing the endianness setting of Exif by opening your jpeg in a hex editor and finding...

  • The bytes 45 78 69 66 (makes "Exif" text) followed by two zero bytes 00 00.
  • Then it should be 49 49 (makes "II" text) which means read data as little endian format.
  • If you replace it with 4D 4D (or "MM" text) then the reading side will consider data as big endian.

Test this in iOS to see if numbers are now correct.

and regarding this...

However, setting 3 shows up as0 on iOS and the image is sideways in Chrome.
Setting 6 shows up as 3 on iOS and the image looks right in Chrome.

Only thing I can add is that iOS Mac is Big Endian* and Android/PC is Little Endian. Essentially Little Endian reads/writes bytes as right-to-left whilst Big Endian is opposite.

In binary : 011 means 3 and 110 means 6. The difference between 3 and 6 is simply the reading order of those bits of ones & zeroes. So a system that reads as zero-one-one gets a result of 3 but the other Endian system will read a byte with same bits as one-one-zero and tell you result is a 6. I can't explain why "3 shows up as 0" without a test file to analyse bytes but it's a strange result to me.

</end rant>
<sleep>

*note: While Macs are Big Endian, double-checking says iOS uses Little Endian system after all. Your numbers still suggest a Big vs Little Endian issue though.

11

Turns out my code was able to set the EXIF data, but there is a discrepancy between how Android interprets this data and how iOS and Chrome on a Mac (where I was checking the resulting file) interprets it.

This is the only code needed to set EXIF orientation:

ExifInterface exif = new ExifInterface(pictureFile.toString());
exif.setAttribute(ExifInterface.TAG_ORIENTATION, "3");
exif.saveAttributes();

However, setting 3 shows up as 0 on iOS and the image is sideways in Chrome.

setting 6 shows up as 3 on iOS and the image looks right in Chrome.

1
  • 1
    +1 for progress on own issue. You should have mentioned the Android / iOS testing issue. Only thing I can add is that iOS is Big Endian and Android is Little Endian. I'll expand in an Answer box...
    – VC.One
    Sep 19, 2016 at 0:31
3

Refer this GitHub project https://github.com/pandiaraj44/Camera. It has the custom camera activity where EXIF TAG_ORIENTATION was handled correctly. You can clone the project and check. For code details please refer https://github.com/pandiaraj44/Camera/blob/master/app/src/main/java/com/pansapp/cameraview/CameraFragment.java

1
  • ExifInterface: Invalid image: ExifInterface got an unsupported image format file(ExifInterface supports JPEG and some RAW image formats only) or a corrupted JPEG file to ExifInterface. when i running your code
    – yarin
    Dec 1, 2017 at 11:06
3

There is one class to read and update these information of images.

To update the attributes you can use like this

ExifInterface ef = new ExifInterface(filePath);
            ef.setAttribute(MAKE_TAG, MAKE_TAG);
            ef.setAttribute(ExifInterface.TAG_ORIENTATION, orientation+"");
            ef.saveAttributes();

and for reading you can use like this

 ExifInterface exif = null;
        try {
            exif = new ExifInterface(absolutePath+path);
        } catch (IOException e) {
            e.printStackTrace();
        }
        int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION,
                ExifInterface.ORIENTATION_UNDEFINED);

I hope it will help you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.