sending an e-mail with multiple attachments

左心房为你撑大大i 提交于 2019-12-04 06:07:55
himen patel

Try This its work fine.

final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND_MULTIPLE);
emailIntent.setType("plain/text");

ArrayList<Uri> uris = new ArrayList<Uri>();

String[] filePaths = new String[] {image1 Path,image2 path};
for (String file : filePaths) {
    File fileIn = new File(file);
    Uri u = Uri.fromFile(fileIn);
    uris.add(u);
}

if ( !(app_preferences.getString("email", "") == null || app_preferences.getString("email", "").equals(""))) {
    emailIntent.putExtra(Intent.EXTRA_EMAIL, new String[] {app_preferences.getString("email", "")});    
}

emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Subject name");
emailIntent.putExtra(Intent.EXTRA_TEXT, "Please find the attachment.");
emailIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);

startActivity(Intent.createChooser(emailIntent, "Email:"));

Tried all this a million times - got it to work but had a nasty warning. Found out it's an Android bug. There's a fix & more info here:

https://code.google.com/p/android/issues/detail?id=38303

Error: ClassCastException warning in log when opening e-mail app with a body and multiple file attachments.

Update: found workaround. Instead of

sendIntent.putExtra(Intent.EXTRA_TEXT, "See attached CSV files.");

Put the text as an ArrayList

ArrayList<String> extra_text = new ArrayList<String>();
extra_text.add("See attached CSV files.");
sendIntent.putStringArrayListExtra(Intent.EXTRA_TEXT, extra_text); 

Voila! No more exception, and the EXTRA_TEXT ends up as the body of the email.

EDIT: I think simply commenting out this line gets rid of the error - but then you don't get to enter any info for a body. In my case that is fine though since I'm only emailing log files. Remove this line to get rid of the warning: 'sendIntent.putExtra(Intent.EXTRA_TEXT, "See attached CSV files.");'

If you want to send some files should pay attention! 1. Use with ACTION_SEND_MULTIPLE instead of ACTION_SEND. 2. Use with setType("text/plain") instead of setType("application/image") 3. Use with putParcelableArrayListExtra(Intent.EXTRA_STREAM, imageUris) instead of putExtra(Intent.EXTRA_STREAM, imageUris)

                Intent emailIntent = new Intent(Intent.ACTION_SEND_MULTIPLE);
                emailIntent.setType("text/plain");
                emailIntent.putExtra(Intent.EXTRA_EMAIL, new String[]{""});
                emailIntent.putExtra(Intent.EXTRA_SUBJECT,"SUBJECT");
                emailIntent.putExtra(Intent.EXTRA_TEXT, "BODY");

                ArrayList<Uri> imageUris = new ArrayList<>();

                imageUris.add(Uri.parse("file://" + invoicePath));
                if (signaturePath != null) {
                    imageUris.add(Uri.parse("file://" + signaturePath));
                }

                emailIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, imageUris);
                startActivity(Intent.createChooser(emailIntent, "Send mail..."));

This works for me.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!