我有这个查询来获取非媒体文件,我想获得光标的正确列但我无法获得列
Uri uri = MediaStore.Files.getContentUri("external");
String[] projection = null;
String selectionMimeType = MediaStore.Files.FileColumns.MIME_TYPE + "=?";
String mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension("pdf");
String[] selectionArgsPdf = new String[]{mimeType};
String sortOrder = MediaStore.Files.FileColumns.MIME_TYPE; // unordered
final Cursor allNonMediaFiles = managedQuery(uri, projection, selectionMimeType, selectionArgsPdf, sortOrder);
我正在使用此代码获取列并获取其中的所有数据,但它无法返回值
int file_column_index = allNonMediaFiles.getColumnIndex(MediaStore.Files.FileColumns._ID);
this.fileCount = allNonMediaFiles.getCount();
this.pdf = new String[this.fileCount];
this.pdfPath = new String[this.fileCount];
for (int i = 0; i < this.fileCount; i++) {
int pdfColumnIndex = allNonMediaFiles.getColumnIndex(MediaStore.Files.FileColumns.TITLE);
pdfPath[i] = allNonMediaFiles.getString(pdfColumnIndex);
}
答案 0 :(得分:1)
确保在遍历游标项目之前执行 cursor.moveToFirst()。
下面的代码工作正常
final Cursor allNonMediaFiles =(getBaseContext()。getContentResolver())。query(uri,projection,selectionMimeType,selectionArgsPdf,sortOrder);
的 allNonMediaFiles.moveToFirst(); 强> 的
int file_column_index = allNonMediaFiles.getColumnIndex(MediaStore.Files.FileColumns._ID);
...
答案 1 :(得分:1)
最后我弄清楚我需要做的就是将光标放在光标的第一位
final Cursor allNonMediaFiles = managedQuery(uri, projection, selectionMimeType, selectionArgsPdf, sortOrder);
int file_column_index = allNonMediaFiles.getColumnIndex(MediaStore.Files.FileColumns._ID);
this.fileCount = allNonMediaFiles.getCount();
this.pdf = new String[this.fileCount];
this.pdfPath = new String[this.fileCount];
for (int i = 0; i < this.fileCount; i++) {
allNonMediaFiles.moveToFirst();
pdfPath[i] = allNonMediaFiles.getString(1);
}