2

i am sending email intent from my app to send a mail through email clients.

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);

My attachment uri content://com.test/logs/test.log

My intent chooser shows the native android email client and gmail client.

I have already asked my questions here but no reply :(

http://productforums.google.com/forum/#!mydiscussions/gmail/3ivhRlbmXc8

Error while sending email with attachment in gmail android app

email client works fine but when i select gmail client it crashes.Below is the stacktrace. The to address is actually empty

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

Thanks and Regards, Saurav

Community
  • 1
  • 1
saurav
  • 5,388
  • 10
  • 56
  • 101

2 Answers2

0

Set your type!

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");
Derek
  • 632
  • 2
  • 11
  • 29
  • 1
    This did not help Derek.Please have a look at my above comment. Problems seem to be the attachments – saurav Jan 17 '14 at 07:35
  • @saurav A downvote is intended to identify answers that are detrimental, not answers that do not directly solve your specific issue. Welcome to SO. – Abandoned Cart May 03 '17 at 18:18
0

I recently discovered a solution for ICS MR1+ while trying to find a way to filter my ACTION_SEND_MULTIPLE to email apps only. First, build your intent like this:

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);

Then, if you need to restrict it to email apps, restrict it like this:

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);
}

This will enable you to send through Gmail without crashing. You may want to do startActivity() in a API check and try/catch, and remove the selector & try again if no activities are found.

Learn OpenGL ES
  • 4,759
  • 1
  • 36
  • 38