Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/335.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在android中发送电子邮件附件_Java_Android_Email_Android Intent - Fatal编程技术网

Java 如何在android中发送电子邮件附件

Java 如何在android中发送电子邮件附件,java,android,email,android-intent,Java,Android,Email,Android Intent,这是我的电子邮件onclick-listner代码: private void sendEmail(){ Intent emailIntent = new Intent(Intent.ACTION_SENDTO); emailIntent.setData(Uri.parse("mailto:" + "recipient@example.com")); emailIntent.putExtra(Intent.EXTRA_SUBJECT, "My email's subject"); email

这是我的电子邮件
onclick
-listner代码:

private void sendEmail(){

Intent emailIntent = new Intent(Intent.ACTION_SENDTO);
emailIntent.setData(Uri.parse("mailto:" + "recipient@example.com")); 
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "My email's subject");
emailIntent.putExtra(Intent.EXTRA_TEXT, "My email's body");

try {
    startActivity(Intent.createChooser(emailIntent, "Send email using..."));
} catch (android.content.ActivityNotFoundException ex) {
    Toast.makeText(Activity.this, "No email clients installed.",   Toast.LENGTH_SHORT).show();
}

}
如何从SD卡中的文件夹发送附件?

请尝试下面的代码

public void clickbutton(View v) {
try {
strFile = Environment.getExternalStorageDirectory()
.getAbsolutePath() + "/temp";

File file = new File(strFile);
if (!file.exists())
file.mkdirs();
strFile = strFile + "/report.html";
createFile();
//
Log.i(getClass().getSimpleName(), "send  task - start");
//
final Intent emailIntent = new Intent(
android.content.Intent.ACTION_SEND);
//
address = "hamdy.ghanem@gmail.com";
subject = "Recite";
emailtext = "Please check the attached recite";
//
emailIntent.setType("plain/text");

emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL,
new String[] { address });

emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject);

emailIntent.putExtra(Intent.EXTRA_STREAM,
Uri.parse("file://" + strFile));

emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, emailtext);

this.startActivity(Intent
.createChooser(emailIntent, "Send mail..."));

} catch (Throwable t) {
Toast.makeText(this, "Request failed: " + t.toString(),
Toast.LENGTH_LONG).show();
}
}public void clickbutton(View v) {
try {
strFile = Environment.getExternalStorageDirectory()
.getAbsolutePath() + "/temp";

File file = new File(strFile);
if (!file.exists())
file.mkdirs();
strFile = strFile + "/report.html";
createFile();
//
Log.i(getClass().getSimpleName(), "send  task - start");
//
final Intent emailIntent = new Intent(
android.content.Intent.ACTION_SEND);
//
address = "hamdy.ghanem@gmail.com";
subject = "Recite";
emailtext = "Please check the attached recite";
//
emailIntent.setType("plain/text");

emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL,
new String[] { address });

emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject);

emailIntent.putExtra(Intent.EXTRA_STREAM,
Uri.parse("file://" + strFile));

emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, emailtext);

this.startActivity(Intent
.createChooser(emailIntent, "Send mail..."));

} catch (Throwable t) {
Toast.makeText(this, "Request failed: " + t.toString(),
Toast.LENGTH_LONG).show();
}
}

在发送文件时查看此链接

,然后需要将
Uri
对象作为
EXTRA\u流
传递给。因此,您需要通过
Uri=Uri.parse(新文件(您的文件路径))
访问它,并且需要使用
emailIntent.putExtra(Intent.EXTRA_-STREAM,Uri)请参考此链接嗨Anjali,您是否在您的终端测试过此代码?这有用吗?看看代码在做什么,问了什么问题。