我一直在开发Android程序,使用Intent
和Intent.ACTION_SEND
使用Intent.putParcelableArrayListExtra(android.content.Intent.EXTRA_STREAM, uri)
发送带有附件(text / plain)的电子邮件我使用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..."));
}
附加到同一邮件,它无法正常工作。没有任何附件出现在电子邮件中。谢谢提前
{{1}}
答案 0 :(得分:2)
使用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);
答案 1 :(得分:0)
确保第二个参数 file 仅提供有效的文件名。你的问题可能就在那里......
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);