问题描述
在类MediaStore.Files类,它提到,
媒体提供商表包含在媒体存储的所有文件的索引,包括非媒体文件
我很感兴趣,在查询非媒体文件(如PDF)。
I'm interested in querying for non-media files like PDF.
我用CursorLoader查询数据库。对于构造函数的第二个参数需要一个开放的参数,它是容易得到的媒体类型的音频,图像和视频为他们每个人都有一个 EXTERNAL_CONTENT_URI
和 INTERNAL_CONTENT_URI
常量定义他们。
I'm using CursorLoader to query the database. The second parameter for the constructor requires an Uri argument which is easy to get for the media types Audio, Images and Video as each of them have a EXTERNAL_CONTENT_URI
and INTERNAL_CONTENT_URI
constant defined for them.
有关MediaStore.Files没有这样定义的常量。我试着用 getContentUri()
的方法,但无法弄清楚的卷名
的参数值。我试图让到/ mnt / SD卡,也有卷名,当我将设备连接到我的系统,但徒劳的出现。
For MediaStore.Files there is no such defined constant. I tried using the getContentUri()
method but couldn't figure out the argument value for volumeName
. I tried giving "/mnt/sdcard" and also the volume name that appears when I connect the device to my system but in vain.
我看到一个在谷歌论坛类似的问题但没有得到解决
I saw a similar question on Google Groups but that is not resolved.
编辑:我也尝试过使用Uri.fromFile(新文件(到/ mnt / SD卡/))和Uri.parse(新文件(到/ mnt / SD卡)的toString()。),但是这并没有无论是工作了。
I also tried using Uri.fromFile(new File("/mnt/sdcard/")) and Uri.parse(new File("/mnt/sdcard").toString()) but that didn't work out either.
推荐答案
是外部
或内部
虽然内部(系统文件)可能是没有用在这里。
It is "external"
or "internal"
although internal (system files) is probably not useful here.
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);
如果你想的.pdf
只有你可以检查MIMETYPE
If you want .pdf
only you could check the 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);
这篇关于MediaStore - 乌里查询所有类型的文件(媒体和非媒体)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!