2016-05-12 151 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 **,那是否設置電子郵件類型? –

相關問題