Trying to attach a file from SD Card to email

70,401

Solution 1

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

Regards,
Fin

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

I hope this helps. It took me ages to debug.

Regards,
Finlay

Solution 2

Just a little remark from my side. I've been having the same issues with GMail, but somehow it seems to work when I store the file in question on the SD card first and retrieve it from there, rather than from the assets. So my code is the following:

Intent i = new Intent(Intent.ACTION_SEND);
i.putExtra(Intent.EXTRA_SUBJECT, "Title");
i.putExtra(Intent.EXTRA_TEXT, "Content");
i.putExtra(Intent.EXTRA_STREAM, uri);
i.setType("text/plain");
startActivity(Intent.createChooser(i, "Send mail"));

and here,

uri = Uri.fromFile(new File(context.getFilesDir(), FILENAME));

does not work, whereas

uri = Uri.fromFile(new File(Environment.getExternalStorageDirectory(), FILENAME));

does.

Regards, Michael

Solution 3

instead of "Uri.parse" use "Uri.fromFile(new File(Environment.getExternalStorageDirectory(),"file name"))"

Environment.getExternalStorageDirectory() - path to SDcard or any other external storage

Solution 4

It appears that this is actually correct, not sure what was happening, but after a reboot it started working :/

Solution 5

    Intent i = new Intent(Intent.ACTION_SEND);
    i.setType("message/rfc822");
    i.putExtra(Intent.EXTRA_EMAIL  , new String[]{"[email protected]"});
    i.putExtra(Intent.EXTRA_SUBJECT, "Data from app");
    i.putExtra(Intent.EXTRA_TEXT   , "experience number x");
    i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 

    Uri uri = Uri.fromFile(new File(Environment.getExternalStorageDirectory(), "filename.txt"));
    i.putExtra(Intent.EXTRA_STREAM, uri);
    startActivity(Intent.createChooser(i, "Send email..."));
Share:
70,401
Chrispix
Author by

Chrispix

Updated on November 07, 2020

Comments

  • Chrispix
    Chrispix over 3 years

    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?

  • Patrick O'Leary
    Patrick O'Leary over 14 years
    I upvoted, but it would be even better if you reworked this so the final solution is worked into the code. The solution is more important than the story here.
  • Chrispix
    Chrispix over 13 years
    I changed the answer to reflect the fact that it needs 3 slashes. I also up voted this.
  • Tyler Collier
    Tyler Collier over 13 years
    I found the same thing. I wonder if it's a permissions issue with Gmail (or other mail program) reading the file? One thing that helped me is that when I used the built-in "Mail" program (not Gmail), it showed that the attachment was 0 bytes. It was nice to know the problem existed without having to send and check my email. :-)
  • mad
    mad over 13 years
    upvote, i would not rework this because people should actually know where the error was. thanks for the solution.
  • Bahadır Yıldırım
    Bahadır Yıldırım over 12 years
    I would suggest replacing startActivity() with startActivityForResult() and removing the file from the root of the SD card in onActivityResult().
  • Bahadır Yıldırım
    Bahadır Yıldırım over 12 years
    The OP's code has a typo: the path should be file:///sdcard (note the three slashes, opposed to two).
  • varun bhardwaj
    varun bhardwaj over 12 years
    upvote, but EXTRA_TEXT not working in case of facebook, only image appears on my wall after post. any idea how do i resolve this.
  • Hoang Tran
    Hoang Tran almost 12 years
    The part about 3 slash is what I want. Thank you.
  • Tom
    Tom almost 11 years
    I think it is not a permission issue. It is an issue with Android, that got corrected somewhere between 2.3 and 4.2. On 2.3, if I make a file on internal storage readable then other apps can open it, but gmail still can't attach it. Needs to be on external storage. On 4.2 I don't have this issue.
  • Carlos Borau
    Carlos Borau over 8 years
    You should not use "hard-coded" paths because they may change depending on the device. Hence I suggest you a slight change in your code: File filelocation = new File(Environment.getExternalStorageDirectory().getAbsolutePa‌​th(), filename); Then define: Uri path = Uri.fromFile(filelocation); and put it in your intent: emailIntent .putExtra(Intent.EXTRA_STREAM, path);