Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 351
Jack Avatar asked Oct 18 '22 08:10

Jack


1 Answers

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.

like image 72
IgorGanapolsky Avatar answered Oct 21 '22 04:10

IgorGanapolsky