使用 EXTRA_STREAM 发送电子邮件 Intent 在 GMail 中崩溃

Sending email Intent with EXTRA_STREAM crashes with GMail

我正在尝试从我的应用程序发送带有附件的电子邮件。当弹出选择器时,如果我点击 GMail,GMail 就会崩溃;如果我点击我设备的默认电子邮件客户端,它会完美运行。附件是从外置sd中截取的jpeg。
这里是代码:

filelocation = gv.getPath();
                    Intent emailIntent = new Intent(Intent.ACTION_SEND);
                    // set the type to 'email'
                    emailIntent.setType("image/jpeg");
                    String to[] = {"myemailaddress@gmail.com"};
                    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:"));

这里是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.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)

有什么建议吗?每一个帮助将不胜感激! :)

这一行:

emailIntent.putExtra(Intent.EXTRA_STREAM, filelocation);

需要 URI 才能工作。更改为:

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