I'm using a function from Microsoft face recognition sample App.
// Get the rotation angle of the image taken.
private static int getImageRotationAngle(
Uri imageUri, ContentResolver contentResolver) throws IOException {
int angle = 0;
Cursor cursor = contentResolver.query(imageUri,
new String[] { MediaStore.Images.ImageColumns.ORIENTATION }, null, null, null);
if (cursor != null) {
if (cursor.getCount() == 1) {
cursor.moveToFirst();
angle = cursor.getInt(0);
}
cursor.close();
} else {
ExifInterface exif = new ExifInterface(imageUri.getPath());
int orientation = exif.getAttributeInt(
ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
switch (orientation) {
case ExifInterface.ORIENTATION_ROTATE_270:
angle = 270;
break;
case ExifInterface.ORIENTATION_ROTATE_180:
angle = 180;
break;
case ExifInterface.ORIENTATION_ROTATE_90:
angle = 90;
break;
default:
break;
}
}
return angle;
}
It works fine in their App, but it throws android.database.CursorIndexOutOfBoundsException: Requested column: 0, # of columns: 0 in the line:
angle = cursor.getInt(0);
The image comes from camera and I have verified that the imageUri is correct. However, the count of cursor is 1, and I can't get anything from it. Does anyone know why?
Update:
content of dump cursor to String
>>>>> Dumping cursor android.content.ContentResolver$CursorWrapperInner@d830286
0 {
_display_name=IMG_344595033.jpg
_size=3335837
}
<<<<<
In the sample App, they used Uri to process the temporarily saved taken photo, but I used file provider since I have SDK 25. It seems that the cursor should be null, but the file provider makes it contains something.
Solved:
I replaced the getCount by getColumnCount, in case that the cursor is empty but getCount returns 1.