我正在从我的应用发送电子邮件意图,通过电子邮件客户端发送邮件。
Intent emailIntent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts("mailto", to[0], null));
emailIntent.putExtra(Intent.EXTRA_SUBJECT, subject);
emailIntent.putExtra(Intent.EXTRA_TEXT, body);
emailIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, attachments);
我的附件uri内容://com.test/logs/test.log
我的意图选择器显示本机Android电子邮件客户端和gmail客户端。
我已经在这里问了我的问题,但没有回复:(
http://productforums.google.com/forum/#!mydiscussions/gmail/3ivhRlbmXc8
Error while sending email with attachment in gmail android app
电子邮件客户端工作正常但是当我选择gmail客户端时它会崩溃.Below是堆栈跟踪。地址实际上是空的
01-16 18:35:37.670: E/AndroidRuntime(6147): FATAL EXCEPTION: main
01-16 18:35:37.670: E/AndroidRuntime(6147): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.google.android.gm/com.google.android.gm.ComposeActivityGmail}: java.lang.NullPointerException
01-16 18:35:37.670: E/AndroidRuntime(6147): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
01-16 18:35:37.670: E/AndroidRuntime(6147): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
01-16 18:35:37.670: E/AndroidRuntime(6147): at android.app.ActivityThread.access$600(ActivityThread.java:141)
01-16 18:35:37.670: E/AndroidRuntime(6147): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
01-16 18:35:37.670: E/AndroidRuntime(6147): at android.os.Handler.dispatchMessage(Handler.java:99)
01-16 18:35:37.670: E/AndroidRuntime(6147): at android.os.Looper.loop(Looper.java:137)
01-16 18:35:37.670: E/AndroidRuntime(6147): at android.app.ActivityThread.main(ActivityThread.java:5103)
01-16 18:35:37.670: E/AndroidRuntime(6147): at java.lang.reflect.Method.invokeNative(Native Method)
01-16 18:35:37.670: E/AndroidRuntime(6147): at java.lang.reflect.Method.invoke(Method.java:525)
01-16 18:35:37.670: E/AndroidRuntime(6147): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
01-16 18:35:37.670: E/AndroidRuntime(6147): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
01-16 18:35:37.670: E/AndroidRuntime(6147): at dalvik.system.NativeStart.main(Native Method)
01-16 18:35:37.670: E/AndroidRuntime(6147): Caused by: java.lang.NullPointerException
01-16 18:35:37.670: E/AndroidRuntime(6147): at android.content.ContentResolver.acquireExistingProvider(ContentResolver.java:1116)
01-16 18:35:37.670: E/AndroidRuntime(6147): at android.content.ContentResolver.getType(ContentResolver.java:257)
01-16 18:35:37.670: E/AndroidRuntime(6147): at com.android.mail.compose.AttachmentsView.m(SourceFile:217)
01-16 18:35:37.670: E/AndroidRuntime(6147): at com.android.mail.compose.ComposeActivity.a(SourceFile:672)
01-16 18:35:37.670: E/AndroidRuntime(6147): at com.android.mail.compose.ComposeActivity.zW(SourceFile:583)
01-16 18:35:37.670: E/AndroidRuntime(6147): at com.android.mail.compose.ComposeActivity.onCreate(SourceFile:445)
01-16 18:35:37.670: E/AndroidRuntime(6147): at com.google.android.gm.ComposeActivityGmail.onCreate(SourceFile:54)
01-16 18:35:37.670: E/AndroidRuntime(6147): at android.app.Activity.performCreate(Activity.java:5133)
01-16 18:35:37.670: E/AndroidRuntime(6147): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
01-16 18:35:37.670: E/AndroidRuntime(6147): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
01-16 18:35:37.670: E/AndroidRuntime(6147): ... 11 more
谢谢和问候, Saurav
答案 0 :(得分:0)
设置你的类型!
Intent emailIntent = new Intent(Intent.ACTION_SEND);
emailIntent.setData(Uri.parse("mailto:"));
emailIntent.setType("text/plain");
emailIntent.putExtra(Intent.EXTRA_EMAIL, TO);
emailIntent.putExtra(Intent.EXTRA_CC, CC);
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Your subject");
emailIntent.putExtra(Intent.EXTRA_TEXT, "Email message goes here");
答案 1 :(得分:0)
我最近发现了一种针对ICS MR1 +的解决方案,同时试图找到一种方法来过滤我的ACTION_SEND_MULTIPLE仅限电子邮件应用。首先,建立你的意图:
sendEmailIntent = new Intent(Intent.ACTION_SEND_MULTIPLE);
sendEmailIntent.setType("message/rfc822");
sendEmailIntent.putExtra(Intent.EXTRA_EMAIL, new String[] { "some@email.com" });
sendEmailIntent.putExtra(Intent.EXTRA_SUBJECT, "Subject");
sendEmailIntent.putExtra(Intent.EXTRA_TEXT, "Body");
final ArrayList<Uri> uris = /* ... Your code to build the attachments. */
sendEmailIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
然后,如果您需要将其限制为电子邮件应用,请将其限制为:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1) {
sendEmailIntent.setType(null); // If we're using a selector, then clear the type to null. I don't know why this is needed, but it doesn't work without it.
final Intent restrictIntent = new Intent(Intent.ACTION_SENDTO);
Uri data = Uri.parse("mailto:?to=some@email.com");
restrictIntent.setData(data);
sendEmailIntent.setSelector(restrictIntent);
}
这使您可以通过Gmail发送而不会崩溃。您可能希望在API检查和try / catch中执行startActivity(),并删除选择器&amp;如果没有找到任何活动,请再试一次。