【问题标题】:Gmail attachment and custom extensionGmail 附件和自定义扩展程序
【发布时间】:2011-09-12 08:13:46
【问题描述】:

我目前正在开发一个读取具有自定义扩展名的文件的 Android 应用程序。 其中一项强制性功能是,当用户收到带有附件 .ourextension 的邮件时,该应用必须由 gmail 提出。

我做了一些研究,发现Android上的gmail客户端不依赖扩展名,因为在启动意图的数据中,提议的文件没有扩展名。它只依赖于邮件客户端给出的mime-type。

问题是我们的自定义文件在邮件客户端之间的检测方式不同。例如,如果我通过 gmail 网页向自己发送我们的自定义文件,则 mime-type 被检测为 application/octet-stream。如果我的朋友使用苹果邮件桌面软件发送,它被检测为文本/xml(这会很好)。而在另一个邮件客户端 Evolution 上,mime-type 是 text/plain...

我们的应用程序无法处理所有这些类型!否则,将针对每种类型的附件提出建议...

有什么解决办法吗?

【问题讨论】:

    标签: android gmail mime-types attachment mime


    【解决方案1】:

    使用 gmail

    发送文件的代码:

    sendIntent.setType("application/calc1");
    

    意图过滤器:

               <!-- Filter to open file with gmail version < 4.2 -->
            <intent-filter>
                <action android:name="android.intent.action.VIEW" />
    
                <category android:name="android.intent.category.DEFAULT" />
    
                <data android:mimeType="application/calc1" />
                <data android:pathPattern=".*\\.calc1" />
                <data android:host="*" />
            </intent-filter>
    
            <!-- Filter to open with file browser or google drive -->
            <intent-filter>
                <action android:name="android.intent.action.VIEW" />
    
                <category android:name="android.intent.category.DEFAULT" />
    
                <data android:scheme="file" />
                <data android:mimeType="*/*" />
                <data android:pathPattern=".*\\.calc1" />
                <data android:host="*" />
            </intent-filter>
    
            <!-- Filter to open file with gmail version 4.2 -->
            <!-- Save file first with "save" button otherwise gmail crashes -->
            <intent-filter>
                <action android:name="android.intent.action.VIEW" />
    
                <category android:name="android.intent.category.DEFAULT" />
    
                <data android:mimeType="application/octet-stream" />
                <data android:pathPattern=".*\\.calc1" />
                <data android:host="*" />
            </intent-filter>
    

    【讨论】:

      【解决方案2】:

      虽然我花了好几天的时间来解决所有问题,但可以通过 gmail 来解决这个问题。

      重要的意图数据

      act=android.intent.action.VIEW
      dat=file:///mnt/sdcard/Download/Availability Focus.inform
      typ=application/octet-stream
      flg=0x80001 cmp=air.com.extension/.AppEntry
      

      能够捕获它的意图过滤器(下面的一些注释)

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

      内容和文件方案都是必需的。如果没有 CONTENT 方案,gmail 将一起忽略您的过滤器。这似乎是一个错误。如果没有 FILE 方案,gmail 会抛出一个错误,说它不知道从意图启动哪个应用程序。

      07-19 15:38:19.160: ERROR/Gmail(2220): Coun't find Activity for intent
      

      两个方案都已就位后,应用程序将接收到已下载文件和已预览文件的意图。必须以不同的方式处理它们。

      【讨论】:

      • 我刚刚对此进行了测试,它适用于在 Android 4.0.3 上运行的 Gmail 4.1.2。但是,当我使用在 Android 2.3.6 上运行的 Gmail 2.3.6 打开同一封电子邮件时,我什至没有打开附件的选项。
      • 在 Android 4.2 上运行的 Gmail 4.2.1 测试。效果很好。谢谢。
      • 测试时,请确保您还尝试打开具有不同扩展名的附件,该附件尚无关联。你会发现你的intent-filter 也匹配它。我认为原因是您没有host 属性,没有pathPattern 将被忽略(developer.android.com/guide/topics/manifest/data-element.html)。如果您添加android:host="*",那么您的intent-filter 将突然不再匹配。问题似乎是content:// URI 不包含文件名(developer.android.com/reference/android/content/ContentUris.html)。
      【解决方案3】:

      您仍然可以匹配 gmail 的扩展名

      <intent-filter>
      <action android:name="android.intent.action.VIEW" />
      <category android:name="android.intent.category.DEFAULT" />
      <data android:mimeType="application/*" host="*" android:pathPattern=".*\\.mht" android:scheme="content" />
      </intent-filter>
      

      将 mime 类型更改为 / 并且它只匹配扩展名

      【讨论】:

      • 对不起,我之前没有看到你的答案,如果有效我会尽快尝试,如果有效则编辑我的答案。
      • 抱歉,这不起作用,Gmail 仅使用 mime 类型作为意图。
      • 为我工作。我有一个下载和预览按钮。在现有的 Galaxy S 手机和 Galaxy S II 平板电脑上进行了测试。
      【解决方案4】:

      编辑:这个答案是 3 岁。我没有时间测试其他人的答案,但我的答案显然已经过时,所以请寻找该主题的其他答案!

      我没有找到任何解决方案,唯一的工作方法是使用 mime-type,所以你必须祈祷你的附件能够被已知的 mime-type 识别,并处理这个问题。这意味着您将干扰其他应用程序,例如,如果您的文件被识别为 html。

      您需要处理您的应用程序无法读取文件的情况,并显示一个弹出窗口以通知用户您无法读取该文件。

      也许一两年后,Gmail 会提供一个很好的 api...

      顺便说一句,最后一个 gmail 版本添加了一个您必须处理以避免崩溃的下载按钮,它通过使用 uri 创建一个新文件来工作。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-03-09
        • 2023-04-09
        • 1970-01-01
        • 2012-03-24
        • 2021-09-14
        • 1970-01-01
        • 2013-06-09
        • 2016-02-19
        相关资源
        最近更新 更多