Android意图:发送带附件的电子邮件

5
我想通过电子邮件发送几个文件。我找到了这个Android多附件使用意图,但它无法工作,我也没有收到任何错误消息。它只是没有附加文件(我尝试发送一个文件,但结果相同)。
我有什么遗漏的吗?您有任何建议吗?
private static void email (Context context, String emailTo, String emailCC, 
    String subject, String emailText, List<String> filePaths)
{
    //need to "send multiple" to get more than one attachment
    final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND_MULTIPLE);
    emailIntent.setType("text/xml");
    emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, 
        new String[]{emailTo});
    emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject);
    emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, emailText);
    //has to be an ArrayList
    ArrayList<Uri> uris = new ArrayList<Uri>();
    //convert from paths to Android friendly Parcelable Uri's
    for (String file : filePaths)
    {
        File fileIn = new File(file);
      //  Uri u = Uri.fromFile(fileIn);
        Uri u = Uri.fromFile(new File(Environment.getExternalStorageDirectory(), "blabla.xml"));
        Log.v("bla", "filepath: " +u.toString());
        uris.add(u);
        Uri b = Uri.fromFile(new File(Environment.getExternalStorageDirectory(), "blabla.es"));
        uris.add(b);
        Log.v("bla", "filepath: " +b.toString());
    }
    emailIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
    context.startActivity(emailIntent);
}

日志记录:

03-06 16:08:50.940: INFO/ActivityManager(69): Starting: Intent { act=android.intent.action.SEND_MULTIPLE typ=text/xml cmp=com.android.email/.activity.MessageCompose (has extras) } from pid 436
03-06 16:08:52.130: INFO/ActivityManager(69): Displayed com.android.email/.activity.MessageCompose: +1s118ms
03-06 16:08:52.470: WARN/IInputConnectionWrapper(436): showStatusIcon on inactive InputConnection

显而易见,但是你确定你的文件路径正确并且指向实际的文件吗? - dymmeh
1
大多数电子邮件程序无法处理 text/xml MIME 类型。此外,大多数用户也无法阅读 XML。请考虑改用 text/plaintext/html 电子邮件正文。 - CommonsWare
1个回答

1

这段代码对我来说是有效的。pdfFiles 的类型是 ArrayList<Uri>

            Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND_MULTIPLE);
            shareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, getText(R.string.share_subject));
            CharSequence seq = Html.fromHtml(mOCRText.toString());
            shareIntent.putExtra(android.content.Intent.EXTRA_TEXT, seq);
            shareIntent.setType("application/pdf");

            shareIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, pdfFiles);
            startActivity(Intent.createChooser(shareIntent, getText(R.string.share_chooser_title)));

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接