在Android File Explorer中按文件类型过滤 [英] Filter by a file type in an Android File Explorer

查看:243
本文介绍了在Android File Explorer中按文件类型过滤的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我想让用户从文件浏览器应用程序中选择文件.通过使用以下代码(在C#/Xamarin中),我已经能够做到这一点:

In my app I want to let the user select files from a file explorer app. I have been able to do this by using the below code (in C# / Xamarin):

private void AddFile()
    {
        if (!IsFileExplorerAppInstalled())
        {
            Toast toast = Toast.MakeText(this, "You must install a File Explorer app to add a file", ToastLength.Long);
            toast.Show();
            return;
        }

        Intent intent = new Intent(Intent.ActionGetContent);
        intent.SetType("file/*");
        StartActivityForResult(intent, IntConstants.GET_FILE_FROM_EXPLORER_KEY);
    }

但是,有时候我希望用户只能选择某种类型的文件,例如mp3文件.是否可以将过滤器列表"传递到文件资源管理器应用程序,或者通常不支持该列表.我找不到任何建议您执行此操作的文档. 谢谢.

However, sometimes I want the user only to be able to select files of a certain type - e.g. mp3 files. Is it possible to pass a 'filter list' to the file explorer app or is it not generally supported. I haven't been able to find any documentation suggesting you can do this. Thanks.

推荐答案

intent.SetType("file/*");表示选择任何文件(由*表示).要仅允许某些文件,请将星号更改为您选择的星号.

intent.SetType("file/*"); means select any file (depicted by the *). To only allow certain files, change the star to that of your choosing.

intent.SetType("audio/mpeg3");

intent.SetType("audio/x-mpeg3"); //Should also work

这是您可以在setType()中设置的常见MIME类型的列表:

Here is the list of common MIME type that you can set in setType():

image/jpeg
audio/mpeg4-generic
text/html
audio/mpeg
audio/aac
audio/wav
audio/ogg
audio/midi
audio/x-ms-wma
video/mp4
video/x-msvideo
video/x-ms-wmv
image/png
image/jpeg
image/gif
.xml ->text/xml
.txt -> text/plain
.cfg -> text/plain
.csv -> text/plain
.conf -> text/plain
.rc -> text/plain
.htm -> text/html
.html -> text/html
.pdf -> application/pdf
.apk -> application/vnd.android.package-archive

您可以在此处找到MIME类型的完整列表:

You can find a full list of MIME types here:

http://www.sitepoint.com/web-foundations/mime-types- complete-list/

这篇关于在Android File Explorer中按文件类型过滤的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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