Android-如何在调用“文件选择器"时仅显示(或能够选择)具有自定义扩展名的文件.使用Intent.ACTION_GET_CONTENT [英] Android - how to only show(or able to select) files with a custom extension when calling "file selector" using Intent.ACTION_GET_CONTENT

查看:121
本文介绍了Android-如何在调用“文件选择器"时仅显示(或能够选择)具有自定义扩展名的文件.使用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>

并使用

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!

推荐答案

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

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.

这篇关于Android-如何在调用“文件选择器"时仅显示(或能够选择)具有自定义扩展名的文件.使用Intent.ACTION_GET_CONTENT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆