MediaStore - 乌里查询所有类型的文件(媒体和非媒体) [英] MediaStore - Uri to query all types of files (media and non-media)

查看:1360
本文介绍了MediaStore - 乌里查询所有类型的文件(媒体和非媒体)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在类<一href="http://developer.android.com/reference/android/provider/MediaStore.Files.html">MediaStore.Files类,它提到,

媒体提供商表包含在媒体存储的所有文件的索引,包括非媒体文件

Media provider table containing an index of all files in the media storage, including non-media 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 - 乌里查询所有类型的文件(媒体和非媒体)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆