Search code examples
androidemailattachmentandroid-intent

Trying to attach a file from SD Card to email


I am trying to launch an Intent to send an email. All of that works, but when I try to actually send the email a couple 'weird' things happen.

here is code

Intent sendIntent = new Intent(Intent.ACTION_SEND);
sendIntent.setType("image/jpeg");
sendIntent.putExtra(Intent.EXTRA_SUBJECT, "Photo");
sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://sdcard/dcim/Camera/filename.jpg"));
sendIntent.putExtra(Intent.EXTRA_TEXT, "Enjoy the photo");
startActivity(Intent.createChooser(sendIntent, "Email:"));

So if I launch using the Gmail menu context It shows the attachment, lets me type who the email is to, and edit the body & subject. No big deal. I hit send, and it sends. The only thing is the attachment does NOT get sent.

So. I figured, why not try it w/ the Email menu context (for my backup email account on my phone). It shows the attachment, but no text at all in the body or subject. When I send it, the attachment sends correctly. That would lead me to believe something is quite wrong. Do I need a new permission in the Manifest launch an intent to send email w/ attachment? What am I doing wrong?


Solution

  • Also getting the same problem

    Code:

    Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND); 
        emailIntent.setType("image/jpeg");
        emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[] 
        {"[email protected]"}); 
        emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, 
        "Test Subject"); 
        emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, 
        "go on read the emails"); 
        Log.v(getClass().getSimpleName(), "sPhotoUri=" + Uri.parse("file:/"+ sPhotoFileName));
        emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:/"+ sPhotoFileName));
        startActivity(Intent.createChooser(emailIntent, "Send mail..."));
    

    From adb logcat:

    V/DumbDumpersMain( 3972):   sPhotoUri=file://sdcard/DumbDumpers/DumbDumper.jpg
    I/ActivityManager(   56):   Starting activity: Intent { action=android.intent.action.CHOOSER comp={android/com.android.internal.app.ChooserActivity} (has extras) }
    I/ActivityManager(   56):   Starting activity: Intent { action=android.intent.action.SEND type=jpeg/image flags=0x3000000 comp={com.google.android.gm/com.google.android.gm.ComposeActivityGmail} (has extras) }
    I/ActivityManager(   56):   Starting activity: Intent { action=android.intent.action.SEND type=jpeg/image flags=0x2800000 comp={com.google.android.gm/com.google.android.gm.ComposeActivity} (has extras) }
    D/gmail-ls(  120):      MailProvider.query: content://gmail-ls/labels/[email protected](null, null)
    D/Gmail   ( 2507):      URI FOUND:file://sdcard/DumbDumpers/DumbDumper.jpg
    

    Looks like the email provider is attaching a 0 length file. When I check the filesystem the file is there and correct. The code which creates the image file is well finished prior to the attempt to email it.

    Anyone fixed this without magic reboots (I've already tried that)?

    Update

    Path for me should have been

    file:///sdcard/DumbDumpers/DumbDumper.jpg

    you need the extra / as this points to the root directory, i.e.:

    file:// + /sdcard/DumbDumpers/DumbDumper.jpg

    combined as

    file:///sdcard/DumbDumpers/DumbDumper.jpg

    In the above snippet you need:

    emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://"+ sPhotoFileName));