Android MediaStore - Uri 查询所有类型的文件(媒体和非媒体)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10384080/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
MediaStore - Uri to query all types of files (media and non-media)
提问by Shyam Prasad Murarka
In the class MediaStore.Filesclass, its mentioned that,
在类MediaStore.Files类中,它提到,
Media provider table containing an index of all files in the media storage, including non-media files.
媒体提供者表包含媒体存储中所有文件的索引,包括非媒体文件。
I'm interested in querying for non-media files like PDF.
我对查询非媒体文件(如 PDF)感兴趣。
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.
我正在使用 CursorLoader 查询数据库。构造函数的第二个参数需要一个 Uri 参数,这对于媒体类型 Audio、Images 和 Video 很容易获取,因为它们中的每一个都有为它们定义的EXTERNAL_CONTENT_URI
和INTERNAL_CONTENT_URI
常量。
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.
对于 MediaStore.Files 没有这样定义的常量。我尝试使用该getContentUri()
方法,但无法找出volumeName
. 我尝试给出“/mnt/sdcard”以及当我将设备连接到系统时出现的卷名,但没有成功。
I saw a similar question on Google Groupsbut that is not resolved.
我在 Google Groups 上看到了类似的问题,但没有解决。
EDIT: 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.
编辑:我也尝试使用 Uri.fromFile(new File("/mnt/sdcard/")) 和 Uri.parse(new File("/mnt/sdcard").toString()) 但这也没有奏效.
回答by zapl
It is "external"
or "internal"
although internal (system files) is probably not useful here.
它是"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);
If you want .pdf
only you could check the mimetype
如果你.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);