在Android文件资源管理器中按文件类型过滤

时间:2015-09-03 09:36:00

标签: android file xamarin

在我的应用中,我想让用户从文件资源管理器应用中选择文件。我已经能够使用下面的代码(在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文件。是否可以将“过滤器列表”传递给文件资源管理器应用程序,或者通常不受支持。我无法找到任何文件表明你可以这样做。 感谢。

2 个答案:

答案 0 :(得分:5)

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

即。

intent.SetType("audio/mpeg3");

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

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

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类型列表:

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

答案 1 :(得分:0)

使用以下代码设置类型:

 intent.setType("file/.mp3");