我一直在研究Android程序,使用Intent
Intent.ACTION_SEND
发送带有附件(文本/纯文本)的电子邮件Intent.putParcelableArrayListExtra(android.content.Intent.EXTRA_STREAM, uri)
但是,当我尝试通过多次调用Intent.putExtra(android.content.Intent.EXTRA_STREAM, uri)
将多个文件附加到同一邮件时,它无法正常工作。电子邮件中未显示任何附件。提前致谢
final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
System.out.println(emailText+emailTo);
emailIntent.setType("text/plain");
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT,emailText);
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject);
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL,new String[]{emailTo});
//has to be an ArrayList
ArrayList<Uri> uris = new ArrayList<Uri>();
//convert from paths to Android friendly Parcelable Uri's
try
{
for (String file : filePaths)
{
File fileIn = new File(context.getFilesDir(),file);
System.out.println(fileIn+"yes");
Uri u = Uri.fromFile(fileIn);
uris.add(u);
System.out.println(u);
}
emailIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM,uris);
context.startActivity(Intent.createChooser(emailIntent, "Send mail..."));
}
使用 ACTION_SEND_MUTIPLE 而不是 ACTION_SEND
http://developer.android.com/reference/android/content/Intent.html#ACTION_SEND_MULTIPLE
final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND_MULTIPLE);
确保第二个参数文件仅提供有效的文件名。 您的问题可能在那里...
File fileIn = new File(context.getFilesDir(),file);
以下代码可以正常工作。
Intent emailIntent = new Intent(Intent.ACTION_SEND_MULTIPLE);
//...
ArrayList<Uri> uris = new ArrayList<Uri>();
uris.add(Uri.parse("file://" + Environment.getExternalStorageDirectory().getPath() + "/HOME/Pictures/1.png"));
uris.add(Uri.parse("file://" + Environment.getExternalStorageDirectory().getPath() + "/HOME/Pictures/2.png"));
uris.add(Uri.parse("file://" + Environment.getExternalStorageDirectory().getPath() + "/HOME/Pictures/3.png"));
uris.add(Uri.parse("file://" + Environment.getExternalStorageDirectory().getPath() + "/HOME/Pictures/4.png"));
emailIntent.putParcelableArrayListExtra(android.content.Intent.EXTRA_STREAM, uris);
startActivity(emailIntent);