+-
Android – 如何使用Intent.ACTION_GET_CONTENT调用“文件选择器”时,只显示(或能够选择)具有自定义扩展名的文件
我知道您可以使用 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>

并使用

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

显示文件选择器,但这导致没有文件扩展名可用,没有文件可以选择:(我真的想不到另一种方式只显示自定义扩展名的文件,提前谢谢!

最佳答案
而不是数据android:mimeType =“application / customtype”,你应该使用数据android:mimeType =“* / *”.这将捕获设备上的操作系统支持的所有mime类型.您根本无法定义操作系统不知道的自定义类型.
点击查看更多相关文章

转载注明原文:Android – 如何使用Intent.ACTION_GET_CONTENT调用“文件选择器”时,只显示(或能够选择)具有自定义扩展名的文件 - 乐贴网