android - 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。
解决方案
消除:
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
,而不仅仅是电子邮件应用程序
推荐阅读
- php - 编码测试时如何让 CodeIgnitor 3 自动加载?
- javascript - vue js 选择(vuetify)+ axios 人口
- csv - 如何在netlogo中对csv文件进行排名
- angular - Angular 组件(带有指令)测试
- reactjs - mapStateToProps 获取 url 参数?
- r - 如何将多个confusionMatrix的输出保存到r中的单个csv文件?
- python - Python中列表的逐步子集化
- javascript - 来自单个按钮的 PHP 2 操作
- php - 如何在 Laravel API 中使用支付网关
- firebase - 使用 FirebaseUI 和 RecyclerView 时出现 NullPointerException