CK8
CK8

Reputation: 359

Android send email with multiple attachments issues

I was able to send email with single attachment. When I switch to send email with multiple attachment, the send mail code doesn't work. Here is my code :

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

I am getting the following errors message. 2019-08-18 11:19:56.736 2979-2979/com.xyz.testemail W/Bundle: Key android.intent.extra.TEXT expected ArrayList but value was a java.lang.String. The default value was returned. 2019-08-18 11:19:56.741 2979-2979/com.xyz.testemail W/Bundle: Attempt to cast generated internal exception: java.lang.ClassCastException: java.lang.String cannot be cast to java.util.ArrayList at android.os.BaseBundle.getCharSequenceArrayList(BaseBundle.java:1265) at android.os.Bundle.getCharSequenceArrayList(Bundle.java:1075) at android.content.Intent.getCharSequenceArrayListExtra(Intent.java:7919) at android.content.Intent.migrateExtraStreamToClipData(Intent.java:10774) at android.app.Instrumentation.execStartActivity(Instrumentation.java:1617) at android.app.Activity.startActivityForResult(Activity.java:4621) at android.support.v4.app.BaseFragmentActivityApi16.startActivityForResult(BaseFragmentActivityApi16.java:54) at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:68) at android.app.Activity.startActivityForResult(Activity.java:4579) at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:751) at android.app.Activity.startActivity(Activity.java:4940) at android.app.Activity.startActivity(Activity.java:4908) at com.xyz.testemail.MainActivity.ShareViaEmail(MainActivity.java:508)

Upvotes: 1

Views: 224

Answers (1)

CommonsWare
CommonsWare

Reputation: 1006574

Remove:

intent.putExtra(Intent.EXTRA_TEXT, message);

If you are using EXTRA_STREAM, you're not supposed to also use EXTRA_TEXT. And, for ACTION_SEND_MULTIPLE, EXTRA_TEXT needs to be a List of strings, not a single string.

Also:

  • Your code will crash on Android 7.0+, because you are using Uri.forFile(). Use FileProvider to make your content available to the other app.

  • No email app has to support ACTION_SEND_MULTIPLE

  • Other apps can support ACTION_SEND_MULTIPLE, not just email apps

Upvotes: 1

Related Questions