如何用Android中的文件附件发送电子邮件
我想将.vcf文件与我的邮件附加在一起并通过邮件发送。但邮件收到的地址没有附件。我已经使用了下面的代码,但代码为此,我不知道我在哪里错了。如何用Android中的文件附件发送电子邮件
try {
String filelocation="/mnt/sdcard/contacts_sid.vcf";
Intent intent = new Intent(Intent.ACTION_SENDTO);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_SUBJECT, "");
intent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://"+filelocation));
intent.putExtra(Intent.EXTRA_TEXT, message);
intent.setData(Uri.parse("mailto:"));
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
activity.startActivity(intent);
activity.finish();
} catch(Exception e) {
System.out.println("is exception raises during sending mail"+e);
}
使用下面的代码来发送邮件
String filename="contacts_sid.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..."));
官方Android site为我工作的例子。 所有什么需要添加
startActivity(Intent.createChooser(emailIntent , "Send email..."));
在阿加瓦尔的答案
在我的情况下,它的邮件客户端,但没有附件。显示的吐司是“无法发送空文件”。我的文件存储在'/ data/data/com.example.app/files/temp.txt'中,我使用'emailIntent.putExtra(Intent.EXTRA_STREAM,Uri.parse(“content:/”+ filePath )); // filePath是/data/com.example.app/files/temp.txt ' – kAmol 2015-09-13 18:00:07
您无法发送文件,因为它位于应用的缓存目录中,只有您的应用可以从该目录读取。您应该使用另一个目录,例如Environment.getExternalStorageDirectory()。 – Borzh 2015-09-28 18:17:46
使用Environment.getExternalStorageDirectory(),验证路径是有效的,该文件具有良好的数据....但仍然得到相同的错误消息(?)。 – CESDewar 2016-03-16 19:14:10
文件夹名所做的就是在手机的内部存储文件的名称。 (ACTUALLY EXTERNAL_STORAGE)。 file_name是您要发送的文件的名称。
private void ShareViaEmail(String folder_name, String file_name) {
try {
File Root= Environment.getExternalStorageDirectory();
String filelocation=Root.getAbsolutePath() + folder_name + "/" + file_name;
Intent intent = new Intent(Intent.ACTION_SENDTO);
intent.setType("text/plain");
String message="File to be shared is " + file_name + ".";
intent.putExtra(Intent.EXTRA_SUBJECT, "Subject");
intent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://"+filelocation));
intent.putExtra(Intent.EXTRA_TEXT, message);
intent.setData(Uri.parse("mailto:[email protected]"));
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
} catch(Exception e) {
System.out.println("is exception raises during sending mail"+e);
}
}
SENDTO不支持附件。我已经使用Provider添加了我的答案以读取文件信息。它在Kotlin。
fun shareFile(context: Context, filePath: File?, fileShareInfo: FileShareInfo) {
val intentFileShare = Intent(Intent.ACTION_SEND)
if (filePath!!.exists()) {
intentFileShare.type = fileShareInfo.fileType
val uri = FileProvider.getUriForFile(context, BuildConfig.APPLICATION_ID + ".provider", filePath)
intentFileShare.putExtra(Intent.EXTRA_STREAM, uri)
fileShareInfo.recipients?.let {
intentFileShare.putExtra(Intent.EXTRA_EMAIL, fileShareInfo.recipients)
}
intentFileShare.putExtra(Intent.EXTRA_SUBJECT, fileShareInfo.shareSubjectText)
fileShareInfo.shareExtraText?.let {
intentFileShare.putExtra(Intent.EXTRA_TEXT, AppViewUtil.fromHtml(fileShareInfo.shareExtraText!!))
}
try {
ContextCompat.startActivity(context, Intent.createChooser(intentFileShare, fileShareInfo.shareTitle), null)
} catch (e: ActivityNotFoundException) {
Toast.makeText(context, context.getString(R.string.sharing_no_app_found), Toast.LENGTH_LONG).show()
}
}
}
一看我的问题... HTTP://stackoverflow.com/questions/12798001/android-how-to-send-multiple-contacts-are-attached-in-single-vcf-file-并发送 – NagarjunaReddy 2012-10-11 06:37:32
您不应使用“硬编码”路径,因为它们可能会因设备而异。我建议你改变文件位置的定义为: File filelocation = new File(Environment.getExternalStorageDirectory()。getAbsolutePath(),filename); 然后定义: Uri path = Uri.fromFile(filelocation);并把它放在你的意图中: emailIntent .putExtra(Intent.EXTRA_STREAM,path); – 2015-11-24 07:52:44
phillip工作正常,emailIntent.putExtra(Intent.EXTRA_STREAM,filelocation)不会为我附加文件,但使用emailIntent.putExtra(Intent.EXTRA_STREAM,Uri.parse(“file://”+ filelocation))。 – andytrombone 2016-01-10 01:18:28