简体   繁体   English

Android:注册意图过滤器以使用我的应用程序打开电子邮件附件

[英]Android: Registering Intent Filter to open email attachment with my app

I have an app that generates custom file types (.sor).我有一个生成自定义文件类型 (.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.这允许我(有时)直接从手机上的用户电子邮件客户端使用应用程序打开文件。

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

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.我唯一能想到的是,当我从手机发送电子邮件时,附件中嵌入的 mime 类型与我在 PC 上从 Thunderbird 发送时不同……我将 mime 类型指定为“应用程序/ octet-stream”,当我从我的应用程序发送电子邮件时,我有一个意图过滤器来查找这种 mime 类型......但它无法正常工作。

My intent filter:我的意图过滤器:

<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"));

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

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.回顾一下,我的手机上有 2 封相同的电子邮件,附加了相同的文件,并且最初都是从我的应用程序发送到同一个帐户(也都是从同一个帐户收到的),但是附件中的附件我的 PC 作为不必要的中间人工作正常,而直接从我的手机发送和接收的 PC 则不能正常工作。

Any ideas?有任何想法吗? Thank you in advance.先感谢您。

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.我已经解决了这个问题,主要是通过在黑暗中拍摄并且没有真正理解我为什么解决了这个问题,但这是我现在在清单中的意图过滤器的内容,其中“.sor”是我的自定义文件的扩展名类型。 This works with all email and file management apps that I have tried, including K-9 mail and Astro:这适用于我尝试过的所有电子邮件和文件管理应用程序,包括 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>

The example of Tim Autin to this question Android intent filter not working worked for me. Tim Autin 对这个问题的示例Android 意图过滤器对我不起作用 He uses three intent filters:他使用了三个意图过滤器:

  • one for supported mime types一种用于支持的 mime 类型
  • one for file extensions with wildcard mime type <data android:mimeType="*/*" />一种用于具有通配符 mime 类型的文件扩展名<data android:mimeType="*/*" />
  • one for file extensions without mime type tag at all一种用于完全没有 mime 类型标签的文件扩展名

Combinations of mime-types and file extensions were to restrictive and ended up not matching my file extension at all mime 类型和文件扩展名的组合受到限制,最终与我的文件扩展名完全不匹配

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM