【问题标题】:Android Intent for File Type Fails文件类型的 Android Intent 失败
【发布时间】:2012-01-29 22:09:57
【问题描述】:

这在之前已经进行了一般性讨论,但我现在有一个具体案例。我的应用程序会打开一个特定于应用程序的文件类型 (.gedstar),它实际上是一个 SQLite 数据库,通常作为带有 pathPattern 限定符的 application/octet-stream 处理。这是我的 Intent 定义:

        <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"
                    android:mimeType="application/octet-stream"
                    android:pathPattern=".*\\.gedstar"
                    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="file"
                    android:mimeType="*/*"
                    android:pathPattern=".*\\.gedstar"
                    android:host="*" />
        </intent-filter>

(我在有和没有通配符的 mimeType 的情况下都有过,顺便说一句)。到目前为止,我一直在推荐使用 Dropbox 将文件从用户的 PC 传输到 Android 并启动我的应用程序,直到最近这一直运行良好。然后,随着 Dropbox V2.0 应用程序的发布,它崩溃了,导致吐司消息说没有此文件类型的应用程序。

用 Logcat 捕获它,这是行不通的意图,在我看来应该是这样:

I/ActivityManager(97):开始活动:Intent { act=android.intent.action.VIEW dat=file:///mnt/sdcard/Android/data/com.dropbox.android/files/scratch/GedStar %20Pro/Gordon.gedstar typ=application/octet-stream flg=0x10000003(有附加功能)}

奇怪的是,我可以使用 Dropbox 的浏览器界面下载相同的文件,然后转到浏览器的下载列表并成功启动它。这是成功的意图:

I/ActivityManager(97):开始活动:Intent { act=android.intent.action.VIEW dat=file:///mnt/sdcard/download/Gordon.gedstar typ=application/octet-stream flg=0x10000000 cmp=com.ghcssoftware.gedstar/.GedStar }

我看到的唯一区别是“flg”的值和“cmp=”的值,我认为这是因为 ActivityManager 找到了匹配的意图。谁能更全面地解释一下?

【问题讨论】:

  • 另一个区别是%20。尝试以没有空格的方式下载 DropBox 中的文件。我没有检查过 DropBox 外部存储文件结构,但我猜测“GedStar Pro”是一个文件夹。
  • 我已经解决了这个问题,但事实证明这是 pathPattern 的限制。请参阅下面的答案。

标签: android


【解决方案1】:

感谢 Dropbox 的技术支持,原来我不是唯一一个遇到此问题的人,原因是 Intent 字符串中的“com.dropbox.android”包含那些额外的点。事实证明 pathPattern 匹配不是“贪婪的”,因此被规范中的第一个点打破。如果用户在目录或文件名中有一个点,它也会中断!解决方案(我们仍然使用“kludge”这个词吗?)是提供一堆带有 pathPatterns 的意图过滤器,例如:

        <intent-filter>
            <action android:name="android.intent.action.VIEW" />
            <category android:name="android.intent.category.DEFAULT" />
            <category android:name="android.intent.category.BROWSABLE" />
            <category android:name="android.intent.category.ALTERNATIVE"/>
            <data android:scheme="file"
                    android:mimeType="application/octet-stream"
                    android:pathPattern=".*\\..*\\..*\\..*\\.gedstar"
                    android:host="*" />
        </intent-filter>

这匹配在“.gedstar”字符串前面带有三个点的路径。这个想法是包括多个意图来覆盖尽可能多的点。丑陋,但它现在可以工作了!

【讨论】:

  • 有什么特殊原因您不只是使用自己的供应商 MIME 类型吗?
  • 它实际上是一个 SQLite 数据库文件,所以我不确定如何让启动活动(例如 Dropbox)将其识别为特定的 MIME 类型。
猜你喜欢
  • 1970-01-01
  • 2016-02-02
  • 1970-01-01
  • 1970-01-01
  • 2012-02-15
  • 2011-12-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多