Gmail 附件和自定义扩展程序

发布于 2024-11-14 23:19:44 字数 440 浏览 4 评论 0原文

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

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

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

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

对此有什么解决方案吗?

I work currently on an Android application that read file with a custom extension.
One of the mandatory features, is that the app must be proposed by gmail when the user receive a mail with the attachment .ourextension.

I did some research, and found that gmail client on Android doesn't rely on the extension, because in the data of the launched intent the file proposed has no extension. It only rely on the mime-type given by the mail client.

The problem is that our custom file are not detected the same way between mail clients. For example, if I send to myself with the gmail webpage our custom file, the mime-type is detect as application/octet-stream. If a friend of mine send with apple mail desktop software, it is detected as a text/xml (which would be nice). And on another mail client, Evolution, the mime-type is text/plain...

Our application can't handle all those types ! Otherwise, it would be proposed for every type of attachment...

Is there any solution for this ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

酸甜透明夹心 2024-11-21 23:19:44

使用 gmail 测试的打开具有自定义扩展名的文件的解决方案 < 4.2、gmail 4.2、Google Drive 和文件浏览器

发送文件的代码:

sendIntent.setType("application/calc1");

Intent-filter :

           <!-- 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>

Solution to open file with custom extension tested with gmail < 4.2, gmail 4.2, Google Drive and file browser

Code to send file :

sendIntent.setType("application/calc1");

Intent-filter :

           <!-- 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>
只有影子陪我不离不弃 2024-11-21 23:19:44

尽管我花了几天的时间来解决所有问题,但可以通过 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

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

It is possible to get this to work from gmail although it's taking me days of effort to figure out all the issues.

The Important Intent Data

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

The Intent Filter that was able to capture it (some notes below)

  <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>

BOTH the content and file scheme's are required. Without the CONTENT scheme gmail will ignore your filter all together. This seems like a bug. Without the FILE scheme gmail will throw an error saying it doesn't know which application to launch from the intent.

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

With both scheme's in place the application receives the intent for both Downloaded and Previewed files. They have to be handled differently.

漆黑的白昼 2024-11-21 23:19:44

您仍然可以匹配 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 类型更改为 / 并且它只会匹配扩展名

You can still match extension for 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>

Make the change for mime type to be / and itll only match on extension

摇划花蜜的午后 2024-11-21 23:19:44

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

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

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

也许在一两年内,Gmail 将提供一个很好的 api...

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

Edit: This answer is 3 years old. I didn't have the time to test the other's answers but mine is clearly outdated so please looks for the others answers of this topic !

I didn't found any solutions, the only way to work is with mime-type, so you must pray for your attachment to be recognize with a known mime-type, and handle this. It means that you will interfer with other application, if your file is recognize as html for example.

You need to handle the case that a file is not readable by your application and show a popup for notifying the user that you can't read the file.

Maybe in a year or two, Gmail will provide a good api...

By the way, the last gmail version add a download button that you must handle to avoid crashes, it works by creating a new file with the uri.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文