[英]ContentResolver.query: CursorIndexOutOfBoundsException
我正在使用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;
}
它在其App中运行良好,但会抛出android.database.CursorIndexOutOfBoundsException:请求的列:0,行中的列数:0:
angle = cursor.getInt(0);
图片来自相机,我已经验证了imageUri是正确的。 但是,游标的计数为1,我无法从中得到任何信息。 有人知道为什么吗?
更新:
转储光标到字符串的内容
>>>>> Dumping cursor android.content.ContentResolver$CursorWrapperInner@d830286
0 {
_display_name=IMG_344595033.jpg
_size=3335837
}
<<<<<
在示例应用程序中,他们使用Uri处理临时保存的照片,但由于使用的是SDK 25,因此我使用了文件提供程序。似乎光标应该为null,但是文件提供程序使其包含某些内容。
解决了:
我将getCount替换为getColumnCount,以防光标为空但getCount返回1的情况。
首先通过在游标上调用getColumnCount()来检查列数。 您也可以通过查看此示例来更改游标的迭代方式。
如果有人像我一样错过了任务的答案,我将其放在此处以节省其他不专心的人的时间。 如果您使用自定义FileProvider,则只需将getCount
替换为getColumnCount
,以防光标为空但getCount返回1的情况。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.