Reputation: 78
I know this has been discussed plenty of times before, and I have looked at many questions and many answers, and I have tried them to no avail, either because of my lack of understanding or my device. Plus, most of the questions are dated by now. I have the classic problem of portrait images coming out rotated to landscape.
Anyways, I have a custom camera that I am using. Here is a function in my main activity:
public static int getImageOrientation(String imagePath){
int orientation = -1;
try {
ExifInterface exif = new ExifInterface(imagePath);
int exifOrientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_NORMAL);
switch (exifOrientation) {
case ExifInterface.ORIENTATION_ROTATE_270:
orientation = 270;
break;
case ExifInterface.ORIENTATION_ROTATE_180:
orientation = 180;
break;
case ExifInterface.ORIENTATION_ROTATE_90:
orientation = 90;
break;
case ExifInterface.ORIENTATION_NORMAL:
orientation = 0;
break;
default:
break;
}
} catch (IOException e) {
Log.e("LOG_TAG", "Unable to get image exif orientation", e);
}
return orientation;
}
This function is called here in my onactivityresult:
String abspath = data.getExtras().getString(EXTRA_IMAGE_PATH);
mMediaUri = Uri.fromFile(new File(abspath));
rotation = getImageOrientation(abspath);
Now, what I am trying to do is, I'm storing the rotation in my backend so that when the image appears in a listview, it will know whether or not to be rotated. The problem is that -1 keeps being returned. What could I be doing wrong? I merely want to know if the photo was taken in portrait or landscape.
Upvotes: 0
Views: 1317
Reputation: 21
It looks like exifOrientation returned by exif.getAttributeInt() is not matched to anyone of values listed in case statement. Please check the value of exifOrientation first.
getAttributeInt(String tag, int defaultValue) returns the integer value of the specified tag, in this case, TAG_ORIENTATION. If there is no such tag in the JPEG file or the value cannot be parsed as integer, return defaultValue.
I cam assume the facts below based on your result:
You can see it with some image viewer tool such as Irfanview with plugin.
Upvotes: 1