views:

4395

answers:

10

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?

+4  A: 

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

Chrispix
A: 

Hello, I try this code. I see attachment on G1 device but when I send email and read it in email client I don't see the attachment. What is wrong?

Is the file on the SD card? Path correct? It should work if you see the attachment on the email client. Are you using Gmail to send? Thanks.
Chrispix
Yes, file is on the SD card, path is correct. I see in email client To,next row is my file - its name.jpg and button Remove, next row is text in email. When I send the email, text is deliver correct but attachment isn't there.
This is my code:super.onCreate(savedInstanceState); Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND); emailIntent.setType("plain/text"); emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "pokus"); emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, "t" ); emailIntent.putExtra(android.content.Intent.EXTRA_STREAM, emailIntent.putExtra(android.content.Intent.EXTRA_STREAM, Uri.parse("file://sdcard/dcim/Camera/1233417122606.jpg")); startActivity(Intent.createChooser(emailIntent, "Send mail...")); }
+3  A: 

Also getting the same problem

Code:

Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND); 
    emailIntent.setType("jpeg/image");
    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 / for the root

so it's like

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

combined is

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

So in my example you need:

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

hope this helps. Took me ages to debug.

Regards,

Finlay

Finlay
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.
Patrick O'Leary
A: 

On my Android the DCIM in the sdcard is in Upper Case. Could this be the problem?

john brohan
A: 

I can't get this to work and I have all the forward slashes in my URI string i.e. file:///sdcard/. Any idea why the size is reported as 0?

Bao-Long Nguyen-Trong
A: 

I`m having the same problem. Any Ideas?

Marcial
A: 

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

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

Snigdha
A: 

hi, I got a question, what would I need to do, if I didn't want to add a specific file like in

"Uri.fromFile(new File(Environment.getExternalStorageDirectory(),"file name"))"

but the file that was just chosen or clicked on?

What I mean is: I am trying to click on an image, which opens a menu with different ways of sharing, e.g. via email and if then click on Email, the email app should open and the image I had clicked on before is directly attached to the email...

I already have the click event and menu, but I don't how to tell the app that the file that was clicked on should be attached to the mail.

Any ideas or help would be great... :) thanks

kivy
+1  A: 

Hi,

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

Michael F