android - 在 Android 应用程序中打开文件的 Intent 过滤器出现问题

标签 android android-intent intentfilter

我正在尝试在 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 时得到的结果:

enter image description here

我做错了什么?

最佳答案

删除:

<data android:host="*" />

来自 <intent-filter>条目和:

<data android:pathPattern=".*\\.conf" />

来自content一个。

文件扩展名在 Android 上使用不多。从 Android Q 开始,文件在 Android 上使用得不多。没有 ContentProvider 的要求将类似文件的扩展名放在 contentUri ,正如您从 Uri 中看到的那样在你的屏幕截图中。

如果您想支持常见的Intent类似 ACTION_VIEW 的操作,您最好的选择是以通用元格式(例如 JSON、XML)保存文件,并使用匹配的文件扩展名,然后拥有您的 <intent-filter>根据相应的 MIME 类型进行过滤。您将需要处理用户选择不是由您的应用程序创建的文件的可能性,尽管从技术上讲,即使使用自定义扩展,您也需要处理该问题。

关于android - 在 Android 应用程序中打开文件的 Intent 过滤器出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56177708/

相关文章:

android - 我正在尝试使用服务录制视频

c# - 如何在 C# -> Java (Android) 之间进行套接字编程

android - 我在适配器中使用上下文时出错,我想在适配器中使用 startActivityForResult(...),我该怎么办?

android - 更改 TabLayout 中选项卡的顺序

java - 如何先更新变量然后在java中执行其余代码

android - 用于在 Android 中启动新 Activity 的多个 Intent 标志

java - Zxing: Activity 获取结果

适用于多种 NFC 类型的 Android Intent 过滤器

Android 短信接收器不工作

android - 蓝牙、wifi 和铃声模式的广播 Intent