2012-04-17 83 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);