12

I'm using the phonegap Android plugin: EmailComposerwithAttachments https://github.com/phonegap/phonegap-plugins/tree/master/Android/EmailComposerWithAttachments and it occurs the following error when executing startActivitywithResult function. I'm using Android 4.2 with Cordova 2.5.0

java.lang.ClassCastException: android.text.SpannableStringBuilder cannot be cast to java.util.ArrayList

// setting attachments
    try {
        JSONArray attachments = parameters.getJSONArray("attachments");
        if (attachments != null && attachments.length() > 0) {
            ArrayList<Uri> uris = new ArrayList<Uri>();
            //convert from paths to Android friendly Parcelable Uri's
            for (int i=0; i<attachments.length(); i++) {
                try {
                    File file = new File(attachments.getString(i));
                    if (file.exists()) {
                        Uri uri = Uri.fromFile(file);
                        uris.add(uri);
                    }
                } catch (Exception e) {
                    LOG.e("EmailComposer", "Error adding an attachment: " + e.toString());
                }
            }
            if (uris.size() > 0) {
                emailIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
            }
        }
    } catch (Exception e) {
        LOG.e("EmailComposer", "Error handling attachments param: " + e.toString());
    }

    this.cordova.startActivityForResult(this, emailIntent, 0);

The following is the complete trace.

04-02 16:34:13.120: W/Bundle(698): Key android.intent.extra.TEXT expected ArrayList<CharSequence> but value was a android.text.SpannableStringBuilder.  The default value <null> was returned.

04-02 16:34:13.150: W/Bundle(698): Attempt to cast generated internal exception:

04-02 16:34:13.150: W/Bundle(698): java.lang.ClassCastException: 
android.text.SpannableStringBuilder cannot be cast to java.util.ArrayList

04-02 16:34:13.150: W/Bundle(698):  at android.os.Bundle.getCharSequenceArrayList(Bundle.java:1326)

04-02 16:34:13.150: W/Bundle(698):  at android.content.Intent.getCharSequenceArrayListExtra(Intent.java:4224)

04-02 16:34:13.150: W/Bundle(698):  at android.content.Intent.migrateExtraStreamToClipData(Intent.java:6682)

04-02 16:34:13.150: W/Bundle(698):  at android.app.Instrumentation.execStartActivity(Instrumentation.java:1410)

04-02 16:34:13.150: W/Bundle(698):  at android.app.Activity.startActivityForResult(Activity.java:3351)

04-02 16:34:13.150: W/Bundle(698):  at android.app.Activity.startActivityForResult(Activity.java:3312)

04-02 16:34:13.150: W/Bundle(698):  at org.apache.cordova.DroidGap.startActivityForResult(DroidGap.java:812)

04-02 16:34:13.150: W/Bundle(698):  at org.apache.cordova.example.EmailComposer.sendEmail(EmailComposer.java:157)

04-02 16:34:13.150: W/Bundle(698):  at org.apache.cordova.example.EmailComposer.execute(EmailComposer.java:36)

04-02 16:34:13.150: W/Bundle(698):  at org.apache.cordova.api.CordovaPlugin.execute(CordovaPlugin.java:61)

04-02 16:34:13.150: W/Bundle(698):  at org.apache.cordova.api.PluginManager.exec(PluginManager.java:220)

04-02 16:34:13.150: W/Bundle(698):  at org.apache.cordova.ExposedJsApi.exec(ExposedJsApi.java:44)

04-02 16:34:13.150: W/Bundle(698):  at android.webkit.WebViewCore.nativeMouseClick(Native Method)

04-02 16:34:13.150: W/Bundle(698):  at android.webkit.WebViewCore.nativeMouseClick(Native Method)

04-02 16:34:13.150: W/Bundle(698):  at android.webkit.WebViewCore.access$6800(WebViewCore.java:57)

04-02 16:34:13.150: W/Bundle(698):  at android.webkit.WebViewCore$EventHub.dispatchWebKitEvent(WebViewCore.java:1806)

04-02 16:34:13.150: W/Bundle(698):  at android.webkit.WebViewInputDispatcher.dispatchWebKitEvent(WebViewInputDispatcher.java:689)

04-02 16:34:13.150: W/Bundle(698):  at android.webkit.WebViewInputDispatcher.dispatchWebKitEvents(WebViewInputDispatcher.java:639)

04-02 16:34:13.150: W/Bundle(698):  at android.webkit.WebViewInputDispatcher.access$800(WebViewInputDispatcher.java:78)

04-02 16:34:13.150: W/Bundle(698):  at android.webkit.WebViewInputDispatcher$WebKitHandler.handleMessage(WebViewInputDispatcher.java:1153)

04-02 16:34:13.150: W/Bundle(698):  at android.os.Handler.dispatchMessage(Handler.java:99)

04-02 16:34:13.150: W/Bundle(698):  at android.os.Looper.loop(Looper.java:137)

04-02 16:34:13.150: W/Bundle(698):  at android.webkit.WebViewCore$WebCoreThread.run(WebViewCore.java:827)

04-02 16:34:13.150: W/Bundle(698):  at java.lang.Thread.run(Thread.java:856)
Mohamed_AbdAllah
  • 5,311
  • 3
  • 28
  • 47
Yang
  • 6,682
  • 20
  • 64
  • 96
  • Something tell me you are not showing the relevant part of the code. Do you set a TEXT extra anywhere? – yarian Apr 02 '13 at 23:57
  • yarian, I took the code unmodified directly from https://github.com/phonegap/phonegap-plugins/tree/master/Android/EmailComposerWithAttachments. – Yang Apr 03 '13 at 00:06
  • What I was saying was it looks like it blows up in the // setting body part of it, not the // setting attachments. – yarian Apr 03 '13 at 00:33
  • Could you add the parameters you are passing in? – yarian Apr 03 '13 at 00:36
  • Here is how I called the function. I debugged it and the code did not throw exception until the last function. window.plugins.emailComposer.showEmailComposerWithCallback(null,"Look at this photo","Take a look at this:",["example@email.com", "johndoe@email.org"],[],[],true,["///mnt/sdcard/MyFolder/resized.png"]); – Yang Apr 03 '13 at 01:08
  • I'm having the same problem, does anyone know of a solution? – Ineze Jul 08 '13 at 13:26
  • did you solved this problem i am facing same problem. Can you suggest some work arounds? – surhidamatya Oct 01 '13 at 12:26

2 Answers2

7

It's down this this bug in Android 4.x

You can work around the problem for plain text emails by replacing this line in EmailComposer.java:

emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, body);

with

ArrayList<String> extra_text = new ArrayList<String>();
extra_text.add(body);
emailIntent.putStringArrayListExtra(android.content.Intent.EXTRA_TEXT, extra_text);

But this won't work for HTML emails because Spanned (returned by Html.fromHtml) is not a subclass of Charsequence. When I tried casting the result of Html.fromHtml() to a string, the tags appeared as part of the text :-(

Also when I tried this, the body of plain text emails to appeared when using the Gmail app but it didn't appear in stock Email app - body was always blank.

DaveAlden
  • 30,083
  • 11
  • 93
  • 155
  • Hi i am having same problem for mail attachment can you please give some idea – surhidamatya Oct 01 '13 at 08:31
  • 11
    Another note for using ArrayList with `putStringArrayListExtra()`, the ClassCastException was gone, but text didn't appear in body of Gmail app for both my Nexus 4 API 19 and HTC Evo API 16. The regular `putExtra()` method caused the exception, but at least the body text appeared. (Sidenote: I was using pure Android, not PhoneGap.) – Anonsage Oct 07 '14 at 15:20
0

Just try a different approach:

Intent emailIntent = new Intent(Intent.ACTION_SENDTO, Uri.parse("mailto:" + email));
emailIntent.putExtra(Intent.EXTRA_SUBJECT, subject);
emailIntent.putExtra(Intent.EXTRA_TEXT, body);
//emailIntent.putExtra(Intent.EXTRA_HTML_TEXT, body); //If you are using HTML in your body text

startActivity(Intent.createChooser(emailIntent, "Chooser Title"));

taken from here: Send Email Intent

ODAXY
  • 335
  • 4
  • 12