2012-04-17 81 views
4

我對發送郵件這樣的代碼:安卓:附加文件到郵件

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(BladeActivity.this, "There are no email clients installed.", Toast.LENGTH_SHORT).show(); 
} 

如何連接到該郵件文件:/sdcard/MyFile.csv

感謝,

回答

6

試試這個:

i.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:///sdcard/MyFile.csv")); 

並確保您有所需的權限來訪問外部存儲。

+0

感謝您的幫助! – Gold 2012-04-17 06:18:57

+0

不客氣:) – fardjad 2012-04-17 06:20:58

+0

永遠不要使用固定路徑'/ sdcard /',因爲例如我的SD卡路徑是'/ mnt/sdcard' – 2012-04-17 08:20:59

1

這裏是代碼附加文件在郵件中...此代碼在我嘗試發送郵件時工作..

Intent sendIntent = new Intent(Intent.ACTION_SEND); 
sendIntent.setType("text/plain"); 
sendIntent.putExtra(Intent.EXTRA_SUBJECT, "Subject of Email"); 
sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("/mnt/sdcard/MyFile.csv")); 
sendIntent.putExtra(Intent.EXTRA_TEXT, "Enjoy the mail"); 
startActivity(Intent.createChooser(sendIntent, "Email:"));  

在mani中添加以下權限fest.xml文件。

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
+0

不要使用固定路徑'/ SD卡/',因爲例如我的SD卡路徑是'到/ mnt/sdcard' – 2012-04-17 06:11:59

+0

@PareshMayani:喔.. sorry.thanks爲了顯示我的錯誤 – 2012-04-17 07:21:16

1

使用此代碼..

Intent intent = new Intent(android.content.Intent.ACTION_SEND); 
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
intent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:/"+arr)); 
intent.setType("image/jpg"); 
startActivity(intent);