简体   繁体   English

ContentResolver.query:CursorIndexOutOfBoundsException

[英]ContentResolver.query: CursorIndexOutOfBoundsException

I'm using a function from Microsoft face recognition sample App. 我正在使用Microsoft人脸识别示例应用程序中的功能。

// 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: 它在其App中运行良好,但会抛出android.database.CursorIndexOutOfBoundsException:请求的列:0,行中的列数:0:

angle = cursor.getInt(0);

The image comes from camera and I have verified that the imageUri is correct. 图片来自相机,我已经验证了imageUri是正确的。 However, the count of cursor is 1, and I can't get anything from it. 但是,游标的计数为1,我无法从中得到任何信息。 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. 在示例应用程序中,他们使用Uri处理临时保存的照片,但由于使用的是SDK 25,因此我使用了文件提供程序。似乎光标应该为null,但是文件提供程序使其包含某些内容。

Solved: 解决了:

I replaced the getCount by getColumnCount, in case that the cursor is empty but getCount returns 1. 我将getCount替换为getColumnCount,以防光标为空但getCount返回1的情况。

Id first check the column count by calling getColumnCount() on the cursor. 首先通过在游标上调用getColumnCount()来检查列数。 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. 如果您使用自定义FileProvider,则只需将getCount替换为getColumnCount ,以防光标为空但getCount返回1的情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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