Filter by a file type in an Android File Explorer

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);
    }

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.

Upvotes: 2

Views: 2616

Answers (2)

John
John

Reputation: 8946

set the type using this code:

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

Upvotes: -1

Smittey
Smittey

Reputation: 2492

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

i.e.

intent.SetType("audio/mpeg3");

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

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

You can find a full list of MIME types here:

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

Upvotes: 6

Related Questions