我知道您可以限制在名为的文件资源管理器中显示的可用文件类型意图.ACTION_GET_CONTENT https://developer.android.com/reference/android/content/Intent.html#ACTION_GET_CONTENT轻松地通过使用设置类型() https://developer.android.com/reference/android/content/Intent.html#setType(java.lang.String),但这只能适用于已知的文件扩展名,如 .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>
and use
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 类型。您根本无法定义操作系统不知道的自定义类型。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)