Android:注册意图过滤器以使用我的应用程序打开电子邮件附件 [英] Android: Registering Intent Filter to open email attachment with my app

查看:25
本文介绍了Android:注册意图过滤器以使用我的应用程序打开电子邮件附件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个生成自定义文件类型 (.sor) 的应用程序.在应用程序中,我有一个功能可以发送一封附有这些文件之一的电子邮件.我还有一个意图过滤器,允许应用程序显示在可以打开此类文件的应用程序列表中.这让我(有时)可以直接从手机上的用户电子邮件客户端使用该应用打开文件.

I have an app that generates custom file types (.sor). Inside the app I have a feature to send an email with one of these files attached. I also have an intent filter to allow the app to show up in the list of apps that can open this type of file. This allows me to (sometimes) open the file with the app right from the users email client on the phone.

但是,这仅在电子邮件来自 PC 电子邮件客户端时有效,而在从手机接收电子邮件时无效.例如,如果我生成这些 .sor 文件之一,然后使用我的应用程序向我自己的电子邮件帐户发送电子邮件,我将在手机上收到电子邮件,但无法使用我的应用程序打开附件......但是, 如果我将电子邮件发送到同一个帐户并在我的 PC(使用 Thunderbird)而不是在手机上打开它,然后将其转发或作为新电子邮件发送到我的手机,我将能够使用相同的电子邮件应用程序在手机上用我的应用程序打开附件...我在这里只谈论一个电子邮件帐户,唯一的区别是电子邮件的发送位置,我的手机或我的 Windows 7 PC.

However, this only works when the email comes from a PC email client, and will not work when the email is received from a phone. For example, if I generate one of these .sor files and then use my app to send an email to my own email account I will receive the email on my phone but will not be able to open the attachment with my app... BUT, if I send the email to the same account and open it on my PC (with Thunderbird) instead of on the phone and then either forward it or send it as a new email to my phone I will be able to use the same email app on the phone to open the attachment with my app... I'm only talking about one email account here, the only difference is where the email was sent from, my phone or my windows 7 PC.

我唯一能想到的是,当我从手机发送电子邮件时,附件中嵌入的 mime 类型与我在 PC 上从 Thunderbird 发送时不同...我将 mime 类型指定为应用程序/八位字节流",当我从我的应用程序发送电子邮件时,我有一个 Intent 过滤器来查找这种 mime 类型......但它无法正常工作.

The only thing I can think of is that when I send the email from the phone a different mime-type is being embedded into the attachment than when I send it from Thunderbird on my PC... I specify the mime type as "application/octet-stream" when I send out the email from my app, and I have an intent filter that looks for this mime type... but it does not work correctly.

我的意图过滤器:

<intent-filter>
  <action android:name="android.intent.action.VIEW" />
  <category android:name="android.intent.category.DEFAULT" />
  <data android:mimeType="application/octet-stream" />
  <data android:scheme="file" />
</intent-filter>

当我从手机发送电子邮件附件中的文件时传递给手机电子邮件客户端的意图:

The intent that I pass to the phones email client when sending the file in an email attachment from the phone:

Intent sendIntent = new Intent(Intent.ACTION_SEND);
sendIntent.setType("application/octet-stream");
sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://" + fullPathString));
sendIntent.putExtra(Intent.EXTRA_SUBJECT, "FiberDroid OTDR Trace File: "" + ContextMenuFileName + """);
sendIntent.putExtra(Intent.EXTRA_TEXT, "This e-mail was sent from the TTI FiberDroid Android application.");
startActivity(Intent.createChooser(sendIntent, "Select E-Mail Application"));

同样,附有文件的电子邮件的发送工作正常……如果我从 PC 电子邮件客户端(例如 Outlook 或 Thunderbird)将同一文件通过电子邮件发送回手机,那么我就可以打开该文件直接从手机电子邮件应用程序使用我的应用程序.问题是,如果我在手机上打开电子邮件而不通过我的 PC 作为中间人,我将无法打开附件,我得到的唯一选项是保存到 SD 卡"...

Again, the sending of the email with the file attached works correctly... and if I email the same file back to the phone from a PC email client (such as outlook or thunderbird) then I am able to open the file with my application directly from the phones email app. The problem is, if I open the email on the phone without going through my PC as a middleman I cannot open the attachment, the only option I am given is "Save to SD Card"...

总结一下,我的手机上有 2 封相同的电子邮件,附有相同的文件,并且最初都是从我的应用程序发送到同一个帐户(也都是从同一个帐户收到的),但其中的附件作为不必要的中间人通过我的 PC 正常工作,而直接从我的手机发送和接收的则无法正常工作.

So to recap, I have 2 emails on my phone that are identical, with the same file attached and both originally sent from my application to the same account (both received from the same account also), but the attachment in the one that went through my PC as an unnecessary middleman works correctly, and the one sent and received directly from my phone does not.

有什么想法吗?提前致谢.

Any ideas? Thank you in advance.

推荐答案

我已经解决了这个问题,主要是在黑暗中拍摄并且没有真正理解我为什么解决了这个问题,但这是我的意图过滤器现在在清单中,其中.sor"是我的自定义文件类型的扩展名.这适用于我尝试过的所有电子邮件和文件管理应用程序,包括 K-9 邮件和 Astro:

I've solved this, mostly by shooting in the dark and without really understand WHY what I did solved it, but here is what I have for my intent filters in the manifest now, where ".sor" is the extension of my custom file type. This works with all email and file management apps that I have tried, including K-9 mail and Astro:

<!-- For email attachments -->
<intent-filter>
   <action android:name="android.intent.action.VIEW" />
   <category android:name="android.intent.category.DEFAULT" />
   <data android:mimeType="application/*" host="*" android:pathPattern=".*.sor" android:scheme="content" />
</intent-filter>

<!--  For file browsers -->
<intent-filter>
   <action android:name="android.intent.action.VIEW"/>
   <category android:name="android.intent.category.DEFAULT"/>
   <data android:mimeType="application/*" host="*" android:pathPattern=".*.sor" android:scheme="file" />
</intent-filter>

这篇关于Android:注册意图过滤器以使用我的应用程序打开电子邮件附件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆