2014-02-27 75 views
3

我有一個HTML字符串,我想附加到郵件作爲文件。我可以將這個字符串保存到一個文件並附加它,但是我想將它保存到一個文件中。我認爲這應該是可能的,但我不知道該怎麼做。這是我的代碼:Android發送郵件附帶字符串

String html = "<html><body><b><bold</b><u>underline</u></body></html>"; 
Intent intent = new Intent(Intent.ACTION_SEND, Uri.parse("mailto:")); 
intent.setType("text/html"); 
intent.putExtra(Intent.EXTRA_SUBJECT, "Subject"); 
intent.putExtra(Intent.EXTRA_TEXT, Html.fromHtml(html)); 

// this is where I want to create attachment 
intent.putExtra(Intent.EXTRA_STREAM, Html.fromHtml(html)); 

startActivity(Intent.createChooser(intent, "Send Email")); 

如何將字符串作爲文件附加到郵件?

+1

附件不是內嵌內容。根據定義,它們是文件。 –

回答

4

該代碼可以節省您添加一個清單使用許可外置SD卡讀取。它會在您的應用私人目錄中的文件目錄中創建一個臨時文件,然後使用您的字符串的內容創建該文件並允許讀取權限,以便可以訪問它。

String phoneDesc = "content string to send as attachment"; 

FileOutputStream fos = null; 
try { 
     fos = openFileOutput("tempFile", Context.MODE_WORLD_READABLE); 
     fos.write(phoneDesc.getBytes(),0,phoneDesc.getBytes().length); 
     fos.flush(); 
     fos.close(); 
} catch (IOException ioe) { 
    ioe.printStackTrace(); 
} 
finally { 
    if (fos != null)try {fos.close();} catch (IOException ie) {ie.printStackTrace();} 
} 
File tempFBDataFile = new File(getFilesDir(),"tempFile"); 
Intent emailClient = new Intent(Intent.ACTION_SENDTO, Uri.parse("[email protected]")); 
emailClient.putExtra(Intent.EXTRA_SUBJECT, "Sample Subject"; 
emailClient.putExtra(Intent.EXTRA_TEXT, "Sample mail body content"); 
emailClient.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(tempFBDataFile));//attachment 
Intent emailChooser = Intent.createChooser(emailClient, "select email client"); 
startActivity(emailChooser); 

當你不需要的文件了這應該被調用。

BufferedWriter bw = null; 
File tempData = new File(getFilesDir(),"tempFile"); 
if (tempData.exists()) { 
    tempData.delete(); 
} 
+2

它說「Context.MODE_WORLD_READABLE」由於安全問題已被棄用。 – OneWorld

-1
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")); 
-1
Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND); 
    emailIntent.setType("png/image"); 
    emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[] { 
        "mail--id" }); 
    emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject); 
    emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, message); 
    Uri uri = Uri.fromFile(new File(Environment 
        .getExternalStorageDirectory(), "/saved_images/MyImage.png")); 
    emailIntent.putExtra(Intent.EXTRA_STREAM, uri); 
    emailIntent.setType("text/plain"); 
    startActivity(emailIntent); 

,不要忘記在manifest文件中添加此下方的權限。

<uses-permission android:name="android.permission.INTERNET" /> 
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> 
<uses-permission android:name="android.permission.READ_INTERNAL_STORAGE"/