Android的意图过滤器为特定的文件扩展名?过滤器、意图、文件扩展名、Android

2023-09-11 11:13:51 作者:贱到骨子里

我希望能够下载一个文件带有特定扩展名从网,并把它传递给我的应用程序来处理它,但我一直没能找出意图过滤器。该文件类型是不包含在MIME类型,我试图用

I want to be able to download a file with a particular extension from the 'net, and have it passed to my application to deal with it, but I haven't been able to figure out the intent filter. The filetype is not included in the mimetypes, and I tried using

<data android:path="*.ext" />

但我无法得到那个工作。

but I couldn't get that to work.

推荐答案

下面是我如何定义我的Andr​​oidManifest.xml我的活动,得到这个工作。

Here is how I defined my activity in my AndroidManifest.xml to get this to work.

<activity android:name="com.keepassdroid.PasswordActivity">
    <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="*/*" />
        <data android:pathPattern=".*\\.kdb" />
        <data android:host="*" />
    </intent-filter>
</activity>

计划 文件表示,当一个本地文件被打开(而不是协议,如HTTP,这应该发生)。

The scheme of file indicates that this should happen when a local file is opened (rather than protocol like HTTP).

MIMETYPE 可设置为 \ * / \ * 来匹配任何MIME类型。

mimeType can be set to \*/\* to match any mime type.

pathPattern 是您指定要匹配的内容扩展(在这个例子中名.kdb )。该。* 开头匹配任何字符SQUENCE。这些字符串需要双转义,所以 \\\\。匹配文字句。然后,你结束你的文件扩展名。一个需要注意的有pathPattern是。* 不是一个贪婪的比赛就像你所期望的,如果这是一个普通的EX pression。这种模式将无法匹配包含一个的路径。名.kdb 。对于这个问题,一个更详细的讨论一种解决方法请参见here

pathPattern is where you specify what extension you want to match (in this example .kdb). The .* at the beginning matches any squence of characters. These strings require double escaping, so \\\\. matches a literal period. Then, you end with your file extension. One caveat with pathPattern is that .* is not a greedy match like you would expect if this was a regular expression. This pattern will fail to match paths that contain a . before the .kdb. For a more detailed discussion of this issue and a workaround see here

最后,根据Android的文档,无论是主机计划所需的 pathPattern 属性的工作,所以才设置为通配符匹配任何东西。

Finally, according to the Android documentation, both host and scheme attributes are required for the pathPattern attribute to work, so just set that to the wildcard to match anything.

现在,如果你选择喜欢琳达文件管理器应用程序中的名.kdb 的文件,我的应用程序显示为一个选项。我要指出,这本身并没有允许你下载这个文件类型在浏览器中,因为这与文件格式只能寄存器。有了像琳达文件管理器应用程序,您的手机本身的电阻一般让您下载任何文件类型。

Now, if you select a .kdb file in an app like Linda File Manager, my app shows up as an option. I should note that this alone does not allow you to download this filetype in a browser, since this only registers with the file scheme. Having an app like Linda File Manager on your phone resisters itself generically allowing you to download any file type.