MediaStore - Uri查询所有类型的文件(媒体和非媒体)

Shy*_*rka 21 android mediastore

在类MediaStore.Files类中,它提到了,

媒体提供程序表,包含媒体存储中所有文件的索引,包括非媒体文件.

我有兴趣查询像PDF这样的非媒体文件.

我正在使用CursorLoader来查询数据库.对于构造函数的第二个参数需要一个开放的参数,它是容易得到媒体类型的音频,图像和视频为他们每个人都有一个EXTERNAL_CONTENT_URI,并INTERNAL_CONTENT_URI为它们定义的常量.

对于MediaStore.Files,没有这样定义的常量.我尝试使用该getContentUri()方法但无法找出参数值volumeName.我尝试给出"/ mnt/sdcard"以及将设备连接到我的系统时出现的卷名但是徒劳无功.

在Google网上论坛上看到了类似的问题但尚未解决.

编辑:我也尝试使用Uri.fromFile(新文件("/ mnt/sdcard /"))和Uri.parse(新文件("/ mnt/sdcard").toString()),但这也没有用.

zap*_*apl 43

它是"external"或者"internal"虽然内部(系统文件)在这里可能没用.

ContentResolver cr = context.getContentResolver();
Uri uri = MediaStore.Files.getContentUri("external");

// every column, although that is huge waste, you probably need
// BaseColumns.DATA (the path) only.
String[] projection = null;

// exclude media files, they would be here also.
String selection = MediaStore.Files.FileColumns.MEDIA_TYPE + "="
        + MediaStore.Files.FileColumns.MEDIA_TYPE_NONE;
String[] selectionArgs = null; // there is no ? in selection so null here

String sortOrder = null; // unordered
Cursor allNonMediaFiles = cr.query(uri, projection, selection, selectionArgs, sortOrder);
Run Code Online (Sandbox Code Playgroud)

如果你.pdf只想要你可以检查mimetype

// only pdf
String selectionMimeType = MediaStore.Files.FileColumns.MIME_TYPE + "=?";
String mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension("pdf");
String[] selectionArgsPdf = new String[]{ mimeType };
Cursor allPdfFiles = cr.query(uri, projection, selectionMimeType, selectionArgsPdf, sortOrder);
Run Code Online (Sandbox Code Playgroud)