0

我有这个用于获取非媒体文件的查询,我想为光标获取正确的列,但我无法获取该列

    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);
    }
4

2 回答 2

1

确保在遍历游标项之前执行cursor.moveToFirst() 。

下面的代码工作正常

最终游标 allNonMediaFiles = (getBaseContext().getContentResolver()).query(uri, projection, selectionMimeType, selectionArgsPdf, sortOrder);

allNonMediaFiles.moveToFirst();

int file_column_index = allNonMediaFiles.getColumnIndex(MediaStore.Files.FileColumns._ID);

...

于 2013-07-10T07:46:41.050 回答
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);
    }
于 2013-07-10T07:59:20.260 回答