Error while sending email with attachment in gmail

2019-09-19 07:13发布

问题:

I have been trying to send an email with attachment. Also looking for only email clients to respond.

I have followed this link Send Email Intent.

Below is my code which does the same

Intent intent = new Intent(Intent.ACTION_SENDTO);
// Set MIME type to email message to avoid other non-email applications showing up

        intent.setData(Uri.parse("mailto:"));
        intent.putExtra(Intent.EXTRA_EMAIL, to);
        intent.putExtra(Intent.EXTRA_SUBJECT, subject);
        intent.putExtra(Intent.EXTRA_TEXT, body);
        intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, attachments);

Doing this, i get the gmail and the android email client in the chooser dialog. When i click on gmail client following error comes. Same is working fine with the android email client.

01-08 13:04:50.385: E/AndroidRuntime(13369): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.google.android.gm/com.google.android.gm.ComposeActivityGmail}: java.lang.NullPointerException
01-08 13:04:50.385: E/AndroidRuntime(13369):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
01-08 13:04:50.385: E/AndroidRuntime(13369):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
01-08 13:04:50.385: E/AndroidRuntime(13369):    at android.app.ActivityThread.access$600(ActivityThread.java:141)
01-08 13:04:50.385: E/AndroidRuntime(13369):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
01-08 13:04:50.385: E/AndroidRuntime(13369):    at android.os.Handler.dispatchMessage(Handler.java:99)
01-08 13:04:50.385: E/AndroidRuntime(13369):    at android.os.Looper.loop(Looper.java:137)
01-08 13:04:50.385: E/AndroidRuntime(13369):    at android.app.ActivityThread.main(ActivityThread.java:5103)
01-08 13:04:50.385: E/AndroidRuntime(13369):    at java.lang.reflect.Method.invokeNative(Native Method)
01-08 13:04:50.385: E/AndroidRuntime(13369):    at java.lang.reflect.Method.invoke(Method.java:525)
01-08 13:04:50.385: E/AndroidRuntime(13369):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
01-08 13:04:50.385: E/AndroidRuntime(13369):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
01-08 13:04:50.385: E/AndroidRuntime(13369):    at dalvik.system.NativeStart.main(Native Method)
01-08 13:04:50.385: E/AndroidRuntime(13369): Caused by: java.lang.NullPointerException
01-08 13:04:50.385: E/AndroidRuntime(13369):    at android.content.ContentResolver.acquireExistingProvider(ContentResolver.java:1116)
01-08 13:04:50.385: E/AndroidRuntime(13369):    at android.content.ContentResolver.getType(ContentResolver.java:257)
01-08 13:04:50.385: E/AndroidRuntime(13369):    at com.android.mail.compose.AttachmentsView.m(SourceFile:217)
01-08 13:04:50.385: E/AndroidRuntime(13369):    at com.android.mail.compose.ComposeActivity.a(SourceFile:664)
01-08 13:04:50.385: E/AndroidRuntime(13369):    at com.android.mail.compose.ComposeActivity.zR(SourceFile:575)
01-08 13:04:50.385: E/AndroidRuntime(13369):    at com.android.mail.compose.ComposeActivity.onCreate(SourceFile:437)
01-08 13:04:50.385: E/AndroidRuntime(13369):    at com.google.android.gm.ComposeActivityGmail.onCreate(SourceFile:54)
01-08 13:04:50.385: E/AndroidRuntime(13369):    at android.app.Activity.performCreate(Activity.java:5133)
01-08 13:04:50.385: E/AndroidRuntime(13369):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
01-08 13:04:50.385: E/AndroidRuntime(13369):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
01-08 13:04:50.385: E/AndroidRuntime(13369):    ... 11 more

cheers, Saurav

回答1:

please have a look

    final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
                      emailIntent.setType("plain/text");
                      emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{"upadhyay.jitesh@gmail.com"});
                     // emailIntent.putExtra(android.content.Intent.EXTRA_BCC, new String[]{etEmail.getText().toString()});
                      emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, getResources().getString(R.string.feedback_for)+"an interesting place");
                      emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, etComment.getText().toString()+"\n\n"+getResources().getString(R.string.rating)+rbRating.getRating()+"\n"+getResources().getString(R.string.my_contact_number)+etPhoneNumber.getText().toString());
                      startActivity(Intent.createChooser(emailIntent, getResources

().getString(R.string.chooser_send_feedback)));

please follow my blog, it will help you

http://upadhyayjiteshandroid.blogspot.in/2013/03/android-using-e-mail-services-for.html