Android - 如何在使用 Intent.ACTION_GET_CONTENT 调用 "file selector" 时仅显示(或能够 select)具有自定义扩展名的文件

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

我知道您可以限制显示在 Intent.ACTION_GET_CONTENT easily by using setType() 调用的文件浏览器中的可用文件类型,但这只能用于已知的文件扩展名,如 .jpg、.pdf、.docx,我什么需要的是仅显示具有 自定义扩展名 的文件,例如 .abc,因此用户只能 select 以 .abc 结尾的文件。找了很久也没找到有效的解决办法;最接近的是创建自定义 MIME 类型,如下所示:

             <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>

并使用

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

显示文件 select 或者,但这导致根本没有可用的文件扩展名,无法 selected :( 我真的想不出另一种方法来只显示带有自定义扩展名的文件,提前致谢!

您应该使用 data android:mimeType="*/*" 而不是 data android:mimeType="application/customtype" 。这将捕获您设备上 OS 支持的所有 mime 类型。您根本无法定义 OS 不知道的自定义类型。