Android - 使用 Intent.ACTION_GET_CONTENT 调用“文件选择器”时如何仅显示(或能够选择)具有自定义扩展名的文件

jac*_*314 5 file-extension android intentfilter android-intent mime-types

我知道您可以通过使用setType()轻松限制Intent.ACTION_GET_CONTENT调用的文件资源管理器中显示的可用文件类型,但这只能适用于我需要的已知文件扩展名,例如 .jpg、.pdf、.docx是只显示具有自定义扩展名的文件,例如 .abc,因此用户只能选择以 .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>
Run Code Online (Sandbox Code Playgroud)

并使用

Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("application/customtype"); 
startActivityForResult(intent,FILE_SELECTOR_REQUEST_CODE);
Run Code Online (Sandbox Code Playgroud)

显示文件选择器,但这会导致根本没有可用的文件扩展名,无法选择任何文件:( 我真的想不出另一种方法来仅显示具有自定义扩展名的文件,提前致谢!