2016-05-12 150 views
9

有两个要求:如何仅使用电子邮件应用程序发送带附件的电子邮件?

  • 电子邮件带有附件
  • Intent选择器,应该只有电子邮件应用程序。

我知道/做到:

  • Intent.ACTION_SENDTOintent.setData(Uri.parse("mailto:"))可以确保有在Intent选择器只电子邮件应用程序,但它不会带来附件(如Gmail它的一些应用程序会,但也有很多应用程序会忽略附件)。

  • Intent.ACTION_SEND可以发送电子邮件附件。但是,在Intent选择器中,将会有应用程序实际上不是电子邮件应用程序,但可以响应Intent.ACTION_SEND。使用intent.setType("message/rfc822")可以减少这些应用程序的数量,但不是全部。

  • 参考答案:https://stackoverflow.com/a/8550043/3952691和几乎成功我的目标。我的代码如下:

    private static void sendFeedbackWithAttachment(Context context, String subject) { 
        Intent intent = new Intent(Intent.ACTION_SENDTO); 
        intent.setData(Uri.parse("mailto:")); 
    
        PackageManager packageManager = context.getPackageManager(); 
        List<ResolveInfo> resolveInfos = packageManager.queryIntentActivities(intent, 0); 
        if (resolveInfos.isEmpty()) { 
         Toast.makeText(context, context.getString(R.string.error_activity_not_found), 
           Toast.LENGTH_SHORT).show(); 
        } else { 
         // ACTION_SEND may be replied by some apps that are not email apps. However, 
         // ACTION_SENDTO doesn't allow us to choose attachment. As a result, we use 
         // an ACTION_SENDTO intent with email data to filter email apps and then send 
         // email with attachment by ACTION_SEND. 
         List<LabeledIntent> intents = new ArrayList<>(); 
         Uri uri = getLatestLogUri(); 
         for (ResolveInfo info : resolveInfos) { 
          Intent i = new Intent(Intent.ACTION_SEND); 
          i.setPackage(info.activityInfo.packageName); 
          i.setClassName(info.activityInfo.packageName, info.activityInfo.name); 
          i.putExtra(Intent.EXTRA_EMAIL, new String[] { Def.Meta.FEEDBACK_EMAIL }); 
          i.putExtra(Intent.EXTRA_SUBJECT, subject); 
          i.putExtra(Intent.EXTRA_STREAM, uri); 
          intents.add(new LabeledIntent(i, info.activityInfo.packageName, 
            info.loadLabel(context.getPackageManager()), info.icon)); 
         } 
         Intent chooser = Intent.createChooser(intents.remove(0), 
           context.getString(R.string.send_feedback_to_developer)); 
         chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, 
           intents.toArray(new LabeledIntent[intents.size()])); 
         context.startActivity(chooser); 
        } 
    } 
    

    然而,在某些设备(例如,MIUI V5小蜜2S,我不知道这是否可以由第三方ROM的影响),结果是空Intent选择器。而且似乎上面的Android 6.0,Intent.EXTRA_INITIAL_INTENTS有一些错误(Custom intent-chooser - why on Android 6 does it show empty cells?,另一个:https://code.google.com/p/android/issues/detail?id=202693)。

因此,我不知道如何实现我的目标。请提前帮助我,谢谢。

+0

“Intent.ACTION_SENDTO与intent.setData(Uri.parse(”电子邮件地址:“))可以确保有规定的故意选择器只通过电子邮件应用程序” - 不,不是的。它会在选择器中选择支持该“意图”结构的应用程序。任何人都可以编写这样的应用程序,它可能不是一个“电子邮件应用程序”。没有关于“电子邮件应用程序”的普遍声明。 – CommonsWare

+0

@CommonsWare是的,我知道。但我在谈论普通情况。我们可以说,一个应用程序,其Intent.ACTION_SEND有一个IntentFilter,数据模式为mailto,如果它是一个正式的应用程序,它应该有能力处理电子邮件事件,因此我们可以将其视为一个“电子邮件应用“。 – ywwynm

+0

@ywwynm使用.setType(“message/rfc822”)或者选择器会显示所有支持发送意图的(许多)应用程序。 – Stanojkovic

回答

-1

尝试这种更高效:

String[] TO = {"[email protected]"}; 
    Uri uri = Uri.parse("mailto:[email protected]") 
      .buildUpon() 
      .appendQueryParameter("subject", "subject") 
      .appendQueryParameter("body", "body") 
      .build(); 
    Intent emailIntent = new Intent(Intent.ACTION_SENDTO, uri); 
    emailIntent.putExtra(Intent.EXTRA_EMAIL, TO); 
    startActivity(Intent.createChooser(emailIntent, "Send mail...")); 
+0

你自己尝试过吗?在选择器中将显示比电子邮件更多的应用程序。而你的意图类型是“纯文本/文本”? – ywwynm

+0

我有更新的代码可以尝试。 \ n发送文件 Uri uri = Uri.fromFile(new File(imagePath)); emailIntent.putExtra(Intent.EXTRA_STREAM,uri); – Gmaster

+0

@ywwynm它工作与否? – Gmaster

-2

有两种方法可以做到这

OPTION 1

Intent emailIntent = new Intent(
      android.content.Intent.ACTION_VIEW); 


    //Option 1 
    Uri data = Uri.parse("mailto:?subject=" + "blah blah subject" 
      + "&body=" + "blah blah body" + "&to=" + "[email protected]"); 
    emailIntent.setData(data); 

    startActivity(Intent.createChooser(emailIntent, "")); 

结果

enter image description here

OPTION 2

它的工作原理perfactly但它不会过滤掉FTP

//Option 2 
    emailIntent = new Intent(
      android.content.Intent.ACTION_SEND); 
    emailIntent.setType("message/rfc822"); 
    final String[] toRecipients = new String[] { "[email protected]", "", }; 
    emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, toRecipients); 
    emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "blah blah subject"); 
    emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, 
      Html.fromHtml("blah blah body")); 

    startActivity(Intent.createChooser(emailIntent, "")); 

结果

enter image description here

这两种方法有细微瑕疵我告诉你两种方式,现在直到你选择一个。

+0

耶稣!你能否仔细看看我的问题?在第1部分:“有两个要求”,我已经发布了我非常需要的东西。那么让我们来看看你的答案:对于选项1,附件在哪里?而选项2,我看到了类似ADM Browser和LAN的东西?他们是电子邮件应用吗哦,我的上帝!!! – ywwynm

0

尝试下面的代码发送邮件

String filename="filename.vcf"; 
File filelocation = new File(Environment.getExternalStorageDirectory().getAbsolutePath(), filename); 
Uri path = Uri.fromFile(filelocation); 
Intent emailIntent = new Intent(Intent.ACTION_SEND); 
// set the type to 'email' 
emailIntent .setType("vnd.android.cursor.dir/email"); 
String to[] = {"[email protected]"}; 
emailIntent .putExtra(Intent.EXTRA_EMAIL, to); 
// the attachment 
emailIntent .putExtra(Intent.EXTRA_STREAM, path); 
// the mail subject 
emailIntent .putExtra(Intent.EXTRA_SUBJECT, "Subject"); 
startActivity(Intent.createChooser(emailIntent , "Send email...")); 
+0

** message/rfc822 **,那是否设置电子邮件类型? –

相关问题