1

I cant send email in android with multiple attachment using Intent chooser. After choosing gmail from chooser .It throws Unfortunately gmail has stopped error. I don't what mistake i have done. Can any one help me?

 final Intent ei = new Intent(Intent.ACTION_SEND_MULTIPLE);
                                        ei.setType("plain/text");
                                        ei.putExtra(Intent.EXTRA_EMAIL, new String[] {"ademo@gmail.com"});
                                        ei.putExtra(Intent.EXTRA_SUBJECT, "SFC App Equiry");
                                        ei.putExtra(android.content.Intent.EXTRA_TEXT,Html.fromHtml("<b>"+"Packing Date"+"</b>&nbsp;"+pdate+"<br><b>"+"Shifting Date"+"</b>&nbsp;"+sdate+"<br><b>"+"Delivery Date"+"</b>&nbsp;"+ddate+"<br><b>"+"Source city"+"</b>&nbsp;"+scity+"<br><b>"+"Destination City"+"</b>&nbsp;"+dcity+"<br><b>"+"Source Property Type"+"</b>&nbsp;"+stprop+"<br><b>"+"Destination Property Type"+"</b>&nbsp;"+dtprop));

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

                                    //uris.parse("file://sdcard/Images/20140812_130215.jpg");
                                    //uris.add("file://sdcard/Images/20140812_130227.jpg");

                                    File fileIn = new File(ppath);
                                    File fileIn1 = new File(ppath1);
                                    File fileIn2 = new File(ppath2);
                                    File fileIn3 = new File(ppath3);
                                    File fileIn4 = new File(ppath4);
                                    File fileIn5 = new File(ppath5);
                                    File fileIn6 = new File(ppath6);

                                    Uri u = Uri.fromFile(fileIn);
                                    Uri u1 = Uri.fromFile(fileIn1);
                                    Uri u2 = Uri.fromFile(fileIn2);
                                    Uri u3 = Uri.fromFile(fileIn3);
                                    Uri u4 = Uri.fromFile(fileIn4);
                                    Uri u5 = Uri.fromFile(fileIn5);
                                    Uri u6 = Uri.fromFile(fileIn6);

                                    uris.add(u);
                                    uris.add(u1);
                                    uris.add(u2);
                                    uris.add(u3);
                                    uris.add(u4);
                                    uris.add(u5);
                                    uris.add(u6);



                                    ei.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
                                    startActivityForResult(Intent.createChooser(ei, "Sending multiple attachment"), 12345);

My stacktrace is:

11-26 10:58:41.791: W/Bundle(19728): Key android.intent.extra.TEXT expected ArrayList<CharSequence> but value was a java.lang.String.  The default value <null> was returned.
11-26 10:58:41.801: W/Bundle(19728): Attempt to cast generated internal exception:
11-26 10:58:41.801: W/Bundle(19728): java.lang.ClassCastException: java.lang.String cannot be cast to java.util.ArrayList
11-26 10:58:41.801: W/Bundle(19728):    at android.os.Bundle.getCharSequenceArrayList(Bundle.java:1367)
11-26 10:58:41.801: W/Bundle(19728):    at android.content.Intent.getCharSequenceArrayListExtra(Intent.java:5265)
11-26 10:58:41.801: W/Bundle(19728):    at android.content.Intent.migrateExtraStreamToClipData(Intent.java:7843)
11-26 10:58:41.801: W/Bundle(19728):    at android.content.Intent.migrateExtraStreamToClipData(Intent.java:7810)
11-26 10:58:41.801: W/Bundle(19728):    at android.app.Instrumentation.execStartActivity(Instrumentation.java:1423)
11-26 10:58:41.801: W/Bundle(19728):    at android.app.Activity.startActivityForResult(Activity.java:3547)
11-26 10:58:41.801: W/Bundle(19728):    at android.app.Activity.startActivityForResult(Activity.java:3508)
11-26 10:58:41.801: W/Bundle(19728):    at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:840)
11-26 10:58:41.801: W/Bundle(19728):    at com.ipot.setringtone.MainActivity.onCreate(MainActivity.java:131)
11-26 10:58:41.801: W/Bundle(19728):    at android.app.Activity.performCreate(Activity.java:5484)
11-26 10:58:41.801: W/Bundle(19728):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
11-26 10:58:41.801: W/Bundle(19728):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2400)
11-26 10:58:41.801: W/Bundle(19728):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2498)
11-26 10:58:41.801: W/Bundle(19728):    at android.app.ActivityThread.access$900(ActivityThread.java:179)
11-26 10:58:41.801: W/Bundle(19728):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1324)
11-26 10:58:41.801: W/Bundle(19728):    at android.os.Handler.dispatchMessage(Handler.java:102)
11-26 10:58:41.801: W/Bundle(19728):    at android.os.Looper.loop(Looper.java:146)
11-26 10:58:41.801: W/Bundle(19728):    at android.app.ActivityThread.main(ActivityThread.java:5641)
11-26 10:58:41.801: W/Bundle(19728):    at java.lang.reflect.Method.invokeNative(Native Method)
11-26 10:58:41.801: W/Bundle(19728):    at java.lang.reflect.Method.invoke(Method.java:515)
11-26 10:58:41.801: W/Bundle(19728):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1288)
11-26 10:58:41.801: W/Bundle(19728):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1104)
11-26 10:58:41.801: W/Bundle(19728):    at dalvik.system.NativeStart.main(Native Method)
11-26 10:58:42.431: W/ResolverActivity(19728): mLaunchedFromPackage=com.ipot.setringtone
11-26 10:58:42.781: I/DVFSHelper(19728): DVFSHelper:: failed to load CFMS
11-26 10:58:42.781: I/DVFSHelper(19728): DVFSHelper:: failed to load CFMS
11-26 10:58:42.781: I/DVFSHelper(19728): DVFSHelper:: failed to load CFMS
11-26 10:58:42.781: I/DVFSHelper(19728): DVFSHelper:: failed to load CFMS
11-26 10:58:42.901: I/Choreographer(19728): Skipped 38 frames!  The application may be doing too much work on its main thread.
11-26 10:58:43.001: D/OpenGLRenderer(19728): Enabling debug mode 0
11-26 10:58:47.641: D/AbsListView(19728): onDetachedFromWindow
Divya
  • 182
  • 1
  • 5
  • 16
  • Please post your stacktrace. – Alok Nair Nov 25 '14 at 12:17
  • 11-25 16:40:09.855: W/IconCustomizer(17966): can't load transform_config.xml 11-25 16:40:09.975: W/Bundle(17966): Key android.intent.extra.TEXT expected ArrayList but value was a java.lang.String. The default value was returned.11-25 16:40:09.985: W/Bundle(17966): Attempt to cast generated internal exception: 11-25 16:40:09.985: W/Bundle(17966): java.lang.ClassCastException: java.lang.String cannot be cast to java.util.ArrayList11-25 16:40:09.985: W/Bundle(17966): at android.os.Bundle.getCharSequenceArrayList(Bundle.java:1331) – Divya Nov 25 '14 at 12:23
  • 11-25 16:40:09.985: W/Bundle(17966): at android.content.Intent.getCharSequenceArrayListExtra(Intent.java:4614) 11-25 16:40:09.985: W/Bundle(17966): at android.content.Intent.migrateExtraStreamToClipData(Intent.java:7098) 11-25 16:40:09.985: W/Bundle(17966): at android.content.Intent.migrateExtraStreamToClipData(Intent.java:7066) 11-25 16:40:09.985: W/Bundle(17966): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1417) 11-25 16:40:09.985: W/Bundle(17966): at android.app.Activity.startActivityForResult(Activity.java:3392) – Divya Nov 25 '14 at 12:24
  • @Divya Change message type from plain/text to message/rfc822. Also intent to android.content.Intent.ACTION_SEND_MULTIPLE. See my answer below and update your code – Alok Nair Nov 25 '14 at 12:24
  • 11-25 16:40:09.985: W/Bundle(17966): at android.app.Activity.startActivityForResult(Activity.java:3353) 11-25 16:40:09.985: W/Bundle(17966): at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:840) 11-25 16:40:09.985: W/Bundle(17966): at com.ipot.setringtone.MainActivity.onCreate(MainActivity.java:129) 11-25 16:40:09.985: W/Bundle(17966): at android.app.Activity.performCreate(Activity.java:5135) – Divya Nov 25 '14 at 12:25
  • 11-25 16:40:09.985: W/Bundle(17966): java.lang.ClassCastException: java.lang.String cannot be cast to java.util.ArrayList 11-25 16:40:09.985: W/Bundle(17966): at android.os.Bundle.getCharSequenceArrayList(Bundle.java:1331) 11-25 16:40:09.985: W/Bundle(17966): at android.content.Intent.getCharSequenceArrayListExtra(Intent.java:4614) 11-25 16:40:09.985: W/Bundle(17966): at android.content.Intent.migrateExtraStreamToClipData(Intent.java:7098) 11-25 16:40:09.985: W/Bundle(17966): at android.content.Intent.migrateExtraStreamToClipData(Intent.java:7066) – Divya Nov 25 '14 at 12:28
  • 1
    @Divya Please try to post long stacktrace in question itself. Either while posting question or by updating the question. Understanding the stacktrace by reading through comments is difficult. – Alok Nair Nov 25 '14 at 12:28
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/65602/discussion-between-alok-nair-and-divya). – Alok Nair Nov 25 '14 at 12:31
  • @Divya Is the issue still there? – Alok Nair Nov 26 '14 at 05:55
  • @Alok Nair Yes. Still there is issue. Cant send mail. – Divya Nov 26 '14 at 06:07
  • @Divya did you make changes that I had suggested? – Alok Nair Nov 26 '14 at 06:10
  • @Divya See my answer posted in the chat window link provided http://chat.stackoverflow.com/rooms/65602/discussion-between-alok-nair-and-divya I think that workaround would solve the issue. Please implement it. I have also updated my answer below. – Alok Nair Nov 26 '14 at 06:22
  • @AlokNair ya.. I think the problem is with path of image file. file://sdcard0/Images/tempbe.jpg is it correct?? – Divya Nov 26 '14 at 06:27
  • @Divya come over to the chat window. Have added solution there. Too long to paste here. There is no issue with image path. You can verify it by checking file content length of a sample file or check if it exists. – Alok Nair Nov 26 '14 at 06:28
  • @Divya yw dear. Nice helping you :) – Alok Nair Nov 28 '14 at 05:54

2 Answers2

5

You can send multiple attachments over mail using the following code:

public boolean sendEmailWithMultipleAttachments(Context context,
                                                        String[] emailTo, String[] emailCC, String[] emailBCC,
                                                        String subject, String emailBody, List filePaths) throws ActivityNotFoundException {

            final Intent emailIntent =
                    new Intent(android.content.Intent.ACTION_SEND_MULTIPLE);
            emailIntent.setType("message/rfc822");
            emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, emailTo);
            emailIntent.putExtra(android.content.Intent.EXTRA_CC, emailCC);
            emailIntent.putExtra(android.content.Intent.EXTRA_BCC, emailBCC);
            emailIntent.putExtra(Intent.EXTRA_SUBJECT, subject);
            ArrayList<String> extra_text = new ArrayList<String>(); 
            extra_text.add("Your text here"); 
            ei.putStringArrayListExtra(Intent.EXTRA_TEXT, extra_text);

            if (filePaths != null) {
                // has to be an ArrayList
                ArrayList uris = new ArrayList();
                // convert from paths to Android friendly Parcelable Uri's
                for (String file : filePaths) {
                    File fileIn = new File(file);
                    if (fileIn.exists()) {
                        Uri u = Uri.fromFile(fileIn);
                        uris.add(u);
                    }
                }
                emailIntent.putParcelableArrayListExtra
                        (Intent.EXTRA_STREAM, uris);
            }
            context.startActivity(Intent.createChooser(emailIntent, "Sent mail"));
            return true;
        }
Alok Nair
  • 3,994
  • 3
  • 24
  • 30
  • HI @Alok can you please help me i am trying to share text with multiple image but i am getting this error "Key android.intent.extra.TEXT expected ArrayList but value was a java.lang.String. The default value was returned." Here is my question http://stackoverflow.com/questions/38293853/using-intent-share-text-with-multiple-image-in-android] – Subho Jul 10 '16 at 16:08
1

I didn't change any code. But after running it today, its working perfectly. Don't know which one wrong or right. Anyway, now its working fine for me, the same code which i posted. Thank You Alok Nair for such a help.

Divya
  • 182
  • 1
  • 5
  • 16
  • HI @Divya can you please help me i am trying to share text with multiple image but i am getting this error "Key android.intent.extra.TEXT expected ArrayList but value was a java.lang.String. The default value was returned." I saw the that you was facing the same type issue, if you can solve my issue then please give a answer. Here is my question http://stackoverflow.com/questions/38293853/using-intent-share-text-with-multiple-image-in-android] – Subho Jul 10 '16 at 16:10