[英]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.