Exif data TAG_ORIENTATION always 0
Asked Answered
K

3

27

I need to know the orientation of an image from the gallery (taken by the camera). My initial approach was to use MediaStore.Images.Media.ORIENTATION which was working for my Droid 1. While testing on the HTC Thunderbolt that phone only saves 0 to that field.

I then moved to reading the exif data:

 ExifInterface exifReader = new ExifInterface(mFilePath);
 exifReader.getAttributeInt(ExifInterface.TAG_ORIENTATION, -1);

This also returns 0 for every image. Anyone have ideas on how to properly get the orientation of a photo take on android?

Kussell answered 7/4, 2011 at 0:35 Comment(1)
K
5

My solution:

Remove any checking for orientation from exif data. I could not find one instance where it was accurate.

Use the standard String[] orientationColumn = {MediaStore.Images.Media.ORIENTATION}; to get an orientation.

If this is 0 use decodeStream...

if(o.outHeight > o.outWidth){
  //set orientation to portrait
}

else it is landscape

Kussell answered 13/4, 2011 at 23:55 Comment(4)
This doesn't work for me because, somehow height, width are properly switched if the image was actually in a different orientation..Modestia
Just to update my own issue, this is how i overcame this problem; https://mcmap.net/q/50209/-images-taken-with-action_image_capture-always-returns-1-for-exifinterface-tag_orientation-on-some-gingerbread-devicesModestia
What if the image is square so width and height are the same but the image is inverted and I want to flip it?Calton
can you post full solution?Dispense
T
30

Here is the code I used onActivityResult() in my activity. The intent returned was for picking an image of the type image/*. Works well for me!

Uri imageUri = intent.getData();
String[] orientationColumn = {MediaStore.Images.Media.ORIENTATION};
Cursor cur = managedQuery(imageUri, orientationColumn, null, null, null);
int orientation = -1;
if (cur != null && cur.moveToFirst()) {
    orientation = cur.getInt(cur.getColumnIndex(orientationColumn[0]));
}  
Matrix matrix = new Matrix();
matrix.postRotate(orientation);
Tucker answered 3/8, 2011 at 18:30 Comment(4)
this approach has worked for me to get the orientation of the image in gallery. Thanks..!! :)Reggi
Thanks ! note that managedQuery is now deprecated. You can replace it with "getApplicationContext().getContentResolver().query(...)".Grano
This is good solution and it works for me when ExifInterface didn't work.Londonderry
On Nougat, with a test image selected from the gallery with a 180 orientation, this is returning 0.Dilapidate
K
5

My solution:

Remove any checking for orientation from exif data. I could not find one instance where it was accurate.

Use the standard String[] orientationColumn = {MediaStore.Images.Media.ORIENTATION}; to get an orientation.

If this is 0 use decodeStream...

if(o.outHeight > o.outWidth){
  //set orientation to portrait
}

else it is landscape

Kussell answered 13/4, 2011 at 23:55 Comment(4)
This doesn't work for me because, somehow height, width are properly switched if the image was actually in a different orientation..Modestia
Just to update my own issue, this is how i overcame this problem; https://mcmap.net/q/50209/-images-taken-with-action_image_capture-always-returns-1-for-exifinterface-tag_orientation-on-some-gingerbread-devicesModestia
What if the image is square so width and height are the same but the image is inverted and I want to flip it?Calton
can you post full solution?Dispense
M
3

This is a bug i found that was related to another android bug.. I found a reasonable solution posted here https://mcmap.net/q/50209/-images-taken-with-action_image_capture-always-returns-1-for-exifinterface-tag_orientation-on-some-gingerbread-devices

Modestia answered 14/1, 2012 at 18:37 Comment(1)
That link posted another link that posted another link. Linkception is a crime here in SO.Calton

© 2022 - 2024 — McMap. All rights reserved.