简体   繁体   中英

ContentResolver.query: CursorIndexOutOfBoundsException

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.

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 .

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM