0

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.

Shiloh_C
  • 197
  • 4
  • 13
  • call for testing purposes `contentResolver.query()` with `null` projection and use `DatabaseUtils#dumpCursor` method to dump your cursor on the `logcat`, what do you see? – pskink Oct 28 '17 at 05:48
  • @pskink I have updated the content. – Shiloh_C Oct 28 '17 at 07:21
  • so `_display_name` and `_size` are the columns you can use in your `selection` argument - you are passing `orientation`, whats your `imageUri` like? – pskink Oct 28 '17 at 07:24
  • The imageUri is like "content://com.cheng.hello.provider/my_images/IMG_1780954340.jpg". I have solved this problem by replacing getCount to getColumnCount, in case the cursor is empty but the count is 1. Thank you so much for the dumping method. It is very useful. – Shiloh_C Oct 28 '17 at 07:37
  • so you have a custom `ContentProvider` (or it is a `FileProvider`) and it simply does not return `orientation` column - it just returns `android.provider.OpenableColumns`, btw you can also use `dumpCurrentRow` if you want just to dump a single current row instead of entire cursor – pskink Oct 28 '17 at 07:41

2 Answers2

0

Id first check the column count by calling getColumnCount() on the cursor. Also you can change the way you are iterating through the cursor by looking at this example.

Ahmed
  • 2,966
  • 7
  • 42
  • 69
0

In case if some one missed the answer in the questing as I'm, I put this here to save time for other not attentive people. If you use custom FileProvider just replace the getCount to getColumnCount, in case that the cursor is empty but getCount returns 1.