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)
显示文件选择器,但这会导致根本没有可用的文件扩展名,无法选择任何文件:( 我真的想不出另一种方法来仅显示具有自定义扩展名的文件,提前致谢!
您应该使用数据 android:mimeType="*/*"而不是数据 android:mimeType="application/ customtype" 。这将捕获您设备上的操作系统支持的所有 mime 类型。您根本无法定义操作系统不知道的自定义类型。
| 归档时间: |
|
| 查看次数: |
2405 次 |
| 最近记录: |