Android 使用查询从本地存储扫描图像文件有时会返回空光标
我正在尝试使用getContentResolver().query(…)从内部或外部存储中列出文件中的所有图像,这就是工作。 奇怪的是,有时候这个方法返回一个空的游标,这怎么会发生呢? 我还尝试了另一个建议,但结果是一样的,有时返回的游标是空的。 有什么想法吗?这是我的密码:Android 使用查询从本地存储扫描图像文件有时会返回空光标,android,Android,我正在尝试使用getContentResolver().query(…)从内部或外部存储中列出文件中的所有图像,这就是工作。 奇怪的是,有时候这个方法返回一个空的游标,这怎么会发生呢? 我还尝试了另一个建议,但结果是一样的,有时返回的游标是空的。 有什么想法吗?这是我的密码: protected Exception doInBackground(Object... params) { try { Cursor cursor = getImageCursor();
protected Exception doInBackground(Object... params) {
try {
Cursor cursor = getImageCursor();
cursor.moveToFirst();
mImageSources.clear();
for (int i = cursor.getCount()-1; i > 0; i--) {
cursor.moveToPosition(i);
mImageSources.add(
new CameraRollItem(
Uri.parse(cursor.getString(1)),
cursor.getString(1),
cursor.getString(3),
i
));
}
cursor.close();
} catch (Exception e) {
e.printStackTrace();
return e;
}
return null;
}
private Cursor getImageCursor() {
Cursor cursor = null;
// It have to be matched with the directory in SDCard
cursor = mContext
.getContentResolver()
.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, null, null, null, null);
// if cursor is null, then try to read image from internal memory
if (cursor == null)
cursor = mContext
.getContentResolver()
.query(MediaStore.Images.Media.INTERNAL_CONTENT_URI, null, null, null, null);
return cursor;
}
在浪费时间解决这个问题之后,我已经找到了答案。根据游标文档,如果使用可重用游标,请不要关闭游标。调用close()方法将设置光标无效