2012-02-27 86 views
4

我通過我的應用程序發送郵件。 爲此,我正在使用以下代碼。如何在Android應用程序中使用發送郵件附加文件?

Intent i = new Intent(Intent.ACTION_SEND); 
i.setType("text/plain"); 
i.putExtra(Intent.EXTRA_EMAIL , new String[]{"[email protected]"}); 
i.putExtra(Intent.EXTRA_SUBJECT, "subject of email"); 
i.putExtra(Intent.EXTRA_TEXT , "body of email"); 
try { 
    startActivity(Intent.createChooser(i, "Send mail...")); 
} catch (android.content.ActivityNotFoundException ex) { 
    Toast.makeText(MyActivity.this, "There are no email clients installed.", Toast.LENGTH_SHORT).show(); 
} 

它只是工作正常,但我想附加一個XML文件。 這可能嗎?怎麼樣?

回答

5

有很多類似的問題在Stack Overflow中已經有完美的解決方案。

你可以看看幾個:hereherehere

解決方案是與電子郵件的意圖使用:一個更putExtra用Key-Extra_Stream和Value-URI到文件

請仔細閱讀FAQ以便了解如何從網站獲得更好的效果。

0

ACTION_SEND_MULTIPLE應該是該操作,然後emailIntent.setType("text/plain");後跟:

ArrayList<Uri> uris = new ArrayList<Uri>(); 
String[] filePaths = new String[] {"sdcard/sample.png", "sdcard/sample.png"}; 
for (String file : filePaths) 
{ 
    File fileIn = new File(file); 
    Uri u = Uri.fromFile(fileIn); 
    uris.add(u); 
} 
emailIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris); 
startActivity(emailIntent); 
0

對於有Gmail發送附件:

  1. 文件應在外部存儲裝置或外部存儲裝置

    創建
    • 爲此,您需要將以下內容添加到Android清單 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    • 通過

      String pathname= Environment.getExternalStorageDirectory().getAbsolutePath();

    • 通過

      File myfile=new File(pathname,filename);

    • 寫創建一個新的文件中獲取外部路徑基於什麼邏輯,你所申請
    • 現在文件意圖

      Intent email=new Intent(android.content.Intent.ACTION_SEND);

      email.setType("plain/text");

    • 將額外

      email.putExtra(Intent.EXTRA_STREAM,Uri.fromFile(myfile));email.putExtra(Intent.EXTRA_SUBJECT, "my email subject"); email.putExtra(Intent.EXTRA_TEXT, "my email text");

    • 啓動活動 startActivity(Intent.createChooser(email, "E-mail"));

3
String pathname= Environment.getExternalStorageDirectory().getAbsolutePath(); 
String filename="/MyFiles/mysdfile.txt"; 
File file=new File(pathname, filename); 
Intent i = new Intent(Intent.ACTION_SEND); 
i.putExtra(Intent.EXTRA_SUBJECT, "Title"); 
i.putExtra(Intent.EXTRA_TEXT, "Content"); 
i.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file)); 
i.setType("text/plain"); 
startActivity(Intent.createChooser(i, "Your email id")); 
相關問題