首页 > 解决方案 > Android 发送带有多个附件的电子邮件问题

问题描述

我能够发送带有单个附件的电子邮件。当我切换到发送带有多个附件的电子邮件时,发送邮件代码不起作用。这是我的代码:

 private void ShareViaEmail(String csvFilePath, String ibiFilePath, String message, String mailTo) {
    try {
        //File Root= Environment.getExternalStorageDirectory();
        //String fileLocation=Root.getAbsolutePath() + folderName + "/" + fileName;
        //Intent intent = new Intent(Intent.ACTION_SENDTO);

        //attach multiple file
        Intent intent = new Intent(Intent.ACTION_SEND_MULTIPLE);

        intent.setType("text/plain");
        //String message="CSV file is " + csvFilePath + ".";
        intent.putExtra(Intent.EXTRA_SUBJECT, "Report ");
        //intent.putExtra(Intent.EXTRA_STREAM, Uri.parse( "file://"+csvFilePath));
        Log.d(LOG_TAG,"ibiFilePath : "+ibiFilePath );
        //attached multiple file
        //followed by:
        ArrayList<Uri> uris = new ArrayList<Uri>();

        uris.add(Uri.fromFile(new File(csvFilePath)));
        uris.add(Uri.fromFile(new File(ibiFilePath)));

        intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);

        intent.putExtra(Intent.EXTRA_TEXT, message);
        intent.setData(Uri.parse("mailto:" + mailTo));
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

        startActivity(intent);
    } catch(Exception e)  {

        Log.d(LOG_TAG,"exception raises during sending mail"+e );
    }
}

我收到以下错误消息。2019-08-18 11:19:56.736 2979-2979/com.xyz.testemail W/Bundle:键 android.intent.extra.TEXT 预期 ArrayList 但值是 java.lang.String。已返回默认值。2019-08-18 11:19:56.741 2979-2979/com.xyz.testemail W/Bundle:尝试转换生成的内部异常:java.lang.ClassCastException:java.lang.String 无法转换为 java.util.ArrayList在 android.os.BaseBundle.getCharSequenceArrayList(BaseBundle.java:1265) 在 android.content.Intent.getCharSequenceArrayListExtra(Intent.java:7919) 在 android.content.getCharSequenceArrayList(Bundle.java:1075)。 Intent.migrateExtraStreamToClipData(Intent.java:10774) 在 android.app.Instrumentation.execStartActivity(Instrumentation.java:1617) 在 android.app.Activity。

标签: androidemailemail-attachmentssend

解决方案


消除:

intent.putExtra(Intent.EXTRA_TEXT, message);

如果您正在使用EXTRA_STREAM,则不应同时使用EXTRA_TEXT. 而且,对于ACTION_SEND_MULTIPLE,EXTRA_TEXT需要是一个List字符串,而不是单个字符串。

还:

  • 您的代码将在 Android 7.0+ 上崩溃,因为您使用的是Uri.forFile(). 用于FileProvider使您的内容可用于其他应用程序。

  • 没有电子邮件应用程序必须支持ACTION_SEND_MULTIPLE

  • 其他应用程序可以支持ACTION_SEND_MULTIPLE,而不仅仅是电子邮件应用程序


推荐阅读