Jack
Jack

Reputation: 5614

Android - how to only show(or able to select) files with a custom extension when calling "file selector" using Intent.ACTION_GET_CONTENT

I know that you can restrict available file types showing up in a file explorer called by Intent.ACTION_GET_CONTENT easily by using setType(), but that can only work for the known file extensions like .jpg, .pdf, .docx, what I need is to only show the files that have a custom extension, like .abc, so the user can only select a file that ends with .abc. I've searched for a long time and still can't find an effective way to solve this; the closest one is to create a custom mime type, like this:

             <intent-filter>
                <action android:name="android.intent.action.VIEW" />
                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE" />
                <data android:scheme="file" />
                <data android:mimeType="application/customtype" />
                <data android:pathPattern=".*\\.abc" />
                <data android:host="*" />
            </intent-filter>
            <intent-filter>
                <action android:name="android.intent.action.VIEW" />
                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE" />
                <data android:scheme="content" />
                <data android:mimeType="application/customtype" />
                <data android:pathPattern=".*\\.abc" />
                <data android:host="*" />
            </intent-filter>

and use

Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("application/customtype"); 
startActivityForResult(intent,FILE_SELECTOR_REQUEST_CODE);

to show the file selector, but this results with no files extensions available at all, no files can be selected :( I really can't think of another way to only show files with custom extensions, Thanks in advance!

Upvotes: 5

Views: 2509

Answers (1)

IgorGanapolsky
IgorGanapolsky

Reputation: 26821

Instead of data android:mimeType="application/customtype", you should use data android:mimeType="*/*". That will catch all mime types that the OS on your device supports. You simply cannot define custom types which the OS doesn't know about.

Upvotes: 4

Related Questions