Android:注册Intent Filter以使用我的应用打开电子邮件附件

时间:2012-09-17 15:49:08

标签: android email android-intent filter attachment

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

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

我唯一能想到的是,当我从手机发送电子邮件时,附件中嵌入了一个不同的mime类型,而不是我在电脑上从Thunderbird发送它时......我将mime类型指定为当我从我的应用程序发送电子邮件时,“application / octet-stream”,我有一个寻找此mime类型的intent过滤器......但它无法正常工作。

我的意图过滤器:

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

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

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)将同一文件发回电话,那么我就可以打开文件我的应用程序直接从手机电子邮件应用程序。问题是,如果我在手机上打开电子邮件而不通过我的电脑作为中间人我无法打开附件,我给出的唯一选择是“保存到SD卡”......

所以回顾一下,我的手机上有2封相同的电子邮件,附带相同的文件,最初都是从我的应用程序发送到同一个帐户(同时也是从同一个帐户收到的),但附件中的附件通过我的电脑作为一个不必要的中间人工作正常,直接从我的手机发送和接收的那个没有。

有什么想法吗?提前谢谢。

2 个答案:

答案 0 :(得分:8)

我已经解决了这个问题,主要是通过在黑暗中拍摄并且没有真正理解为什么我所做的解决了它,但这里是我现在对清单中的意图过滤器所做的,其中“.sor”是我的自定义文件类型。这适用于我尝试过的所有电子邮件和文件管理应用程序,包括K-9邮件和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>

答案 1 :(得分:0)

Tim Autin 对这个问题的示例 Android intent filter not working 对我有用。他使用了三个意图过滤器:

  • 支持的 MIME 类型之一
  • 一种用于带有通配符 mime 类型 <data android:mimeType="*/*" /> 的文件扩展名
  • 一个用于完全没有 mime 类型标签的文件扩展名

mime 类型和文件扩展名的组合受到限制,最终与我的文件扩展名完全不匹配

相关问题