Search code examples
javaandroidandroid-intentemail-attachments

Unable to find working solution with Intent.ACTION_SEND_MULTIPLE for multiple images to be attached to an email


So here's the situation - I'm developing an Unreal plugin with native android features. Intention for single image works perfectly, but now, when I'm trying to add multiple image attachments using ACTION_SEND_MULTIPLE it's not starting activity.

No errors, execution stops on .startActivity(), wrapping with try-catch doesn't return any exceptions, Unreal passes images array without any issue. I feel like it's not building intent properly, but after 2 days of searching and countless hours of trials and errors I feel like it's time to give up and seek for advise here :)

Here's the java part of the code I'm suspecting isn't working:

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);
    }
}

tried removing all of the extras, except images, but that didn't solve the problem.

Help would be much appreciated!


Solution

  • After more digging through SO found similar post, changed .fromFile() to FileProvider and it worked like a charm.

    Snippet:

    for (String extraImagePath : extraImagePaths) {
                Uri arrayPath = FileProvider.getUriForFile(activity, getAuthority(activity), new File(extraImagePath));
                paths.add(arrayPath);
            }
    

    P.S. Credit goes to CommonsWare!