无法为要附加到电子邮件的多个图像找到 Intent.ACTION_SEND_MULTIPLE 的工作解决方案
Unable to find working solution with Intent.ACTION_SEND_MULTIPLE for multiple images to be attached to an email
情况是这样的——我正在开发一个具有原生 android 功能的 Unreal 插件。
Intention 对单个图像的处理非常完美,但是现在,当我尝试使用 ACTION_SEND_MULTIPLE
添加多个图像附件时,它没有开始 activity。
没有错误,执行在 .startActivity()
停止,用 try-catch 包装不会 return 任何异常,Unreal 传递图像数组没有任何问题。
我觉得它没有正确构建意图,但经过 2 天的搜索和无数小时的试验和错误后,我觉得是时候放弃并在这里寻求建议了:)
这是我怀疑无法正常工作的 java 代码部分:
public static void sendEMail(Activity activity, String subject, String[] extraImagePaths,
String[] recipients, String[] cc, String[] bcc,
boolean withChooser, String chooserTitle) {
Intent intent = createEMailIntent(subject, recipients, cc, bcc);
ArrayList<Uri> paths = new ArrayList<Uri>();
if (extraImagePaths.length > 0) {
for (String extraImagePath : extraImagePaths) {
File fileIn = new File(extraImagePath);
Uri arrayPath = Uri.fromFile(fileIn);
paths.add(arrayPath);
}
}
intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, paths);
try {
launchShareIntent(activity, withChooser, chooserTitle, intent);
} catch (Exception e) {
Log.d("AndroidLOG:", e.getMessage());
}
}
private static Intent createEMailIntent(String subject, String[] recipients, String[] cc, String[] bcc) {
return new Intent(Intent.ACTION_SEND_MULTIPLE)
.setData(Uri.parse("mailto:"))
.setType("image/*")
.putExtra(Intent.EXTRA_SUBJECT, subject)
.putExtra(Intent.EXTRA_EMAIL, recipients)
.putExtra(Intent.EXTRA_CC, cc)
.putExtra(Intent.EXTRA_BCC, bcc);
}
private static void launchShareIntent(Activity activity, boolean withChooser, String chooserTitle, Intent intent) {
if (withChooser) {
Intent chooserIntent = Intent.createChooser(intent, chooserTitle);
activity.startActivity(chooserIntent);
} else {
activity.startActivity(intent);
}
}
尝试删除除图像之外的所有额外内容,但这并没有解决问题。
不胜感激!
在深入挖掘 SO 后发现类似 post,将 .fromFile()
更改为 FileProvider
,效果非常好。
片段:
for (String extraImagePath : extraImagePaths) {
Uri arrayPath = FileProvider.getUriForFile(activity, getAuthority(activity), new File(extraImagePath));
paths.add(arrayPath);
}
P.S。感谢 CommonsWare!
情况是这样的——我正在开发一个具有原生 android 功能的 Unreal 插件。
Intention 对单个图像的处理非常完美,但是现在,当我尝试使用 ACTION_SEND_MULTIPLE
添加多个图像附件时,它没有开始 activity。
没有错误,执行在 .startActivity()
停止,用 try-catch 包装不会 return 任何异常,Unreal 传递图像数组没有任何问题。
我觉得它没有正确构建意图,但经过 2 天的搜索和无数小时的试验和错误后,我觉得是时候放弃并在这里寻求建议了:)
这是我怀疑无法正常工作的 java 代码部分:
public static void sendEMail(Activity activity, String subject, String[] extraImagePaths,
String[] recipients, String[] cc, String[] bcc,
boolean withChooser, String chooserTitle) {
Intent intent = createEMailIntent(subject, recipients, cc, bcc);
ArrayList<Uri> paths = new ArrayList<Uri>();
if (extraImagePaths.length > 0) {
for (String extraImagePath : extraImagePaths) {
File fileIn = new File(extraImagePath);
Uri arrayPath = Uri.fromFile(fileIn);
paths.add(arrayPath);
}
}
intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, paths);
try {
launchShareIntent(activity, withChooser, chooserTitle, intent);
} catch (Exception e) {
Log.d("AndroidLOG:", e.getMessage());
}
}
private static Intent createEMailIntent(String subject, String[] recipients, String[] cc, String[] bcc) {
return new Intent(Intent.ACTION_SEND_MULTIPLE)
.setData(Uri.parse("mailto:"))
.setType("image/*")
.putExtra(Intent.EXTRA_SUBJECT, subject)
.putExtra(Intent.EXTRA_EMAIL, recipients)
.putExtra(Intent.EXTRA_CC, cc)
.putExtra(Intent.EXTRA_BCC, bcc);
}
private static void launchShareIntent(Activity activity, boolean withChooser, String chooserTitle, Intent intent) {
if (withChooser) {
Intent chooserIntent = Intent.createChooser(intent, chooserTitle);
activity.startActivity(chooserIntent);
} else {
activity.startActivity(intent);
}
}
尝试删除除图像之外的所有额外内容,但这并没有解决问题。
不胜感激!
在深入挖掘 SO 后发现类似 post,将 .fromFile()
更改为 FileProvider
,效果非常好。
片段:
for (String extraImagePath : extraImagePaths) {
Uri arrayPath = FileProvider.getUriForFile(activity, getAuthority(activity), new File(extraImagePath));
paths.add(arrayPath);
}
P.S。感谢 CommonsWare!