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

本文是小编为大家收集整理的关于安卓。注册意图过滤器,用我的应用程序打开电子邮件附件的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我有一个生成自定义文件类型(.sor)的应用程序.在应用程序内部,我有一个功能,可以发送带有其中一个文件的电子邮件.我还有一个意图过滤器,可以允许该应用显示在可以打开此类型文件的应用程序列表中.这使我(有时)可以通过电话上的用户电子邮件客户端打开该文件.

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

我唯一能想到的是,当我从手机发送电子邮件时,与我从PC上从Thunderbird发送的时,将不同的哑光类型嵌入附件...我将MIME类型指定为当我从应用中发送电子邮件时," application/ocket-stream",我有一个查找此MIME类型的意图过滤器...但是它无法正常工作.

我的意图过滤器:

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

因此,回顾一下,我的手机上有2封电子邮件相同,附有相同的文件,并且两者最初从我的申请发送到同一帐户(也从同一帐户中收到),但是一个附件中的附件这是我的PC作为不必要的中间人正常工作的,直接从我的手机发送和收到的中间人也没有.

有什么想法吗?预先感谢您.

推荐答案

我已经解决了这个问题,主要是通过在黑暗中拍摄,并且不理解我为何解决了问题,但这是我现在在清单中的意图过滤器," .sor"是我的自定义文件类型的扩展.这与我尝试过的所有电子邮件和文件管理应用程序一起使用,包括K-9 Mail和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>

其他推荐答案

tim Autin的示例 android意图过滤不起作用对我有用.他使用三个意图过滤器:

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

MIME类型和文件扩展名的组合是限制性的,最终根本不匹配我的文件扩展名

本文地址:https://www.itbaoku.cn/post/102619.html

问题描述

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.

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.

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.

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

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.

推荐答案

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>

其他推荐答案

The example of Tim Autin to this question Android intent filter not working worked for me. He uses three intent filters:

  • one for supported mime types
  • one for file extensions with wildcard mime type <data android:mimeType="*/*" />
  • one for file extensions without mime type tag at all

Combinations of mime-types and file extensions were to restrictive and ended up not matching my file extension at all