在Android应用中打开文件的意图过滤器出现问题 [英] Issue with intent filter for opening a file in Android app
问题描述
我正在尝试在我的android应用中打开任何扩展名为.conf
的文件.这是我的AndroidManifest.xml
中的内容:
I'm trying to open any file with a .conf
extension in my android app. Here's what I have in my 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
文件时,它显示无法打开文件".
But, when I tap on a .conf
file in the Downlaods folder, it says "Can't open file."
这就是我使用意图拦截:
我在做什么错了?
推荐答案
删除:
<data android:host="*" />
来自<intent-filter>
条目和
<data android:pathPattern=".*\\.conf" />
来自content
之一.
文件扩展名在Android上使用不多.从Android Q开始,文件在Android上使用不多.无需ContentProvider
在content
Uri
上放置类似文件的扩展名,如您在屏幕快照中的Uri
所示.
File extensions are not used much on Android. Starting with Android Q, files are not used much on Android. There is no requirement for a ContentProvider
to put a file-like extension on a content
Uri
, as you can see from the Uri
in your screenshot.
如果您希望支持像ACTION_VIEW
这样的常见Intent
动作,最好的选择是将文件保存为具有匹配文件扩展名的通用元格式(例如JSON,XML),然后使用<intent-filter>
过滤相应的MIME类型.您将需要解决用户选择的不是由您的应用创建的文件的可能性,尽管从技术上讲,即使使用自定义扩展名,您也需要处理该文件.
If you wish to support common Intent
actions like ACTION_VIEW
, your best bet is to save the file in a common meta-format (e.g., JSON, XML) with a file extension that matches, then have your <intent-filter>
filter on the corresponding MIME type. You will need to deal with the possibility that the user chooses a file that was not created by your app, though technically you need to deal with that even with your custom extension.
这篇关于在Android应用中打开文件的意图过滤器出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!