我正在尝试在 Android 应用程序中打开任何带有 .conf
扩展名的文件。这是我的 AndroidManifest.xml
中的内容:
<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:host="*" />
<data android:mimeType="*/*" />
<data android:pathPattern=".*\\.conf" />
</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:host="*" />
<data android:mimeType="*/*" />
<data android:pathPattern=".*\\.conf" />
</intent-filter>
但是,当我点击 Downlaods 文件夹中的 .conf
文件时,它显示“无法打开文件。”
这是我使用 Intent Intercept 时得到的结果:
我做错了什么?
最佳答案
删除:
<data android:host="*" />
来自 <intent-filter>
条目和:
<data android:pathPattern=".*\\.conf" />
来自content
一个。
文件扩展名在 Android 上使用不多。从 Android Q 开始,文件在 Android 上使用得不多。没有 ContentProvider
的要求将类似文件的扩展名放在 content
上Uri
,正如您从 Uri
中看到的那样在你的屏幕截图中。
如果您想支持常见的Intent
类似 ACTION_VIEW
的操作,您最好的选择是以通用元格式(例如 JSON、XML)保存文件,并使用匹配的文件扩展名,然后拥有您的 <intent-filter>
根据相应的 MIME 类型进行过滤。您将需要处理用户选择不是由您的应用程序创建的文件的可能性,尽管从技术上讲,即使使用自定义扩展,您也需要处理该问题。
关于android - 在 Android 应用程序中打开文件的 Intent 过滤器出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56177708/