Search code examples
androidemailgmailsendemail-attachments

Sending email Intent with EXTRA_STREAM crashes with GMail


I'm trying to send an email from my app with attachment. When the chooser pops up, if I tap on GMail, GMail crashes; if I tap on the default email client of my device, it works perfectly. The attachment is a jpeg taken from the external sd.
Here the code:

filelocation = gv.getPath();
                    Intent emailIntent = new Intent(Intent.ACTION_SEND);
                    // set the type to 'email'
                    emailIntent.setType("image/jpeg");
                    String to[] = {"[email protected]"};
                    emailIntent.putExtra(Intent.EXTRA_EMAIL, to);
                    // the attachment
                    emailIntent.putExtra(Intent.EXTRA_STREAM, filelocation);
                    // the mail subject
                    emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Subject");
                    emailIntent.putExtra(Intent.EXTRA_TEXT, "Body");
                    emailIntent.setType("message/rfc822");
                    startActivity(Intent.createChooser(emailIntent, "Send email using:"));

Here the logcat:

03-14 19:03:26.081  27428-27428/com.android.myapp W/Bundle﹕ Key android.intent.extra.STREAM expected Parcelable but value was a java.lang.String.  The default value <null> was returned.
03-14 19:03:26.111  27428-27428/com.android.myapp W/Bundle﹕ Attempt to cast generated internal exception:
    java.lang.ClassCastException: java.lang.String cannot be cast to android.os.Parcelable
            at android.os.Bundle.getParcelable(Bundle.java:1171)
            at android.content.Intent.getParcelableExtra(Intent.java:4493)
            at android.content.Intent.migrateExtraStreamToClipData(Intent.java:7032)
            at android.content.Intent.migrateExtraStreamToClipData(Intent.java:7017)
            at android.app.Instrumentation.execStartActivity(Instrumentation.java:1548)
            at android.app.Activity.startActivityForResult(Activity.java:3409)
            at android.app.Activity.startActivityForResult(Activity.java:3370)
            at android.support.v4.app.FragmentActivity.startActivityFromFragment(FragmentActivity.java:826)
            at android.support.v4.app.Fragment.startActivity(Fragment.java:896)
            at com.android.myapp.steps.Passo3$2.onClick(Passo3.java:100)
            at android.view.View.performClick(View.java:4107)
            at android.view.View$PerformClick.run(View.java:17160)
            at android.os.Handler.handleCallback(Handler.java:615)
            at android.os.Handler.dispatchMessage(Handler.java:92)
            at android.os.Looper.loop(Looper.java:155)
            at android.app.ActivityThread.main(ActivityThread.java:5536)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1074)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:841)
            at dalvik.system.NativeStart.main(Native Method)

Any suggestion? Every help will be greatly appreciated! :)


Solution

  • This line:

    emailIntent.putExtra(Intent.EXTRA_STREAM, filelocation);
    

    Need URI to work. Change to:

    emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(filelocation));