0

I am using below code to send email to gmail.Here , i am directly open compose mail of the gmail.it's worked fine.but i need to attache file to that mail.How can i do ? Please can any one help me?

Here my code is:

Intent send = new Intent(Intent.ACTION_SENDTO);
            String uriText = "mailto:" + Uri.encode("user@gmail.com") + 
                      "?subject=" + Uri.encode("Testing app") + 
                      "&body=" + Uri.encode("Hi,this is android app testing");
            Uri uri = Uri.parse(uriText);

            send.setData(uri);
            startActivity(Intent.createChooser(send, "Send mail..."));

Thanking in Advance.

suman
  • 75
  • 3
  • 8
  • you can search on stackoverlow itself. there are lots of similar posts. http://stackoverflow.com/questions/9466169/how-to-attach-files-with-sending-mail-in-android-application – Raghunandan May 20 '13 at 08:16

1 Answers1

1

try the following:

Uri.fromFile(file);
send.putExtra(Intent.EXTRA_STREAM, uri);

EDIT try this one then:

intent.setType("application/zip"); //if it's a zip otherwise whatever you file formap is.
intent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://" + zipDestinationString));

if you wish to use gmail you need to specific the exact intent you want to use:

intent.setClassName("com.google.android.gm", "com.google.android.gm.ComposeActivityGmail");

//note one might generate an exception so you should catch the exception and try the other.


intent.setClassName("com.google.android.gm", "com.google.android.gm.ConversationListActivity");

Here is also a CodeProject sample app that does that: Code Project Send Mail With Attachment Example

Dory Zidon
  • 10,497
  • 2
  • 25
  • 39
  • file attachement worked with bellow code . but it's open gmail,email,Skype etc.. but i need directly compose mail . Intent email = new Intent(Intent.ACTION_SEND); email.setType("message/rfc822"); email.putExtra(Intent.EXTRA_EMAIL, new String[] { "suman.chidurala546@gmail.com" }); email.putExtra(Intent.EXTRA_SUBJECT, "Resume"); email.putExtra(Intent.EXTRA_TEXT, "Please find attachment"); email.putExtra(Intent.EXTRA_STREAM,Uri.fromFile(myFile)); startActivity(Intent.createChooser(email , "Email: Text File")); – suman May 20 '13 at 09:00
  • I do not understand, what is the problem then? Try to revise your question better. – Dory Zidon May 20 '13 at 09:05
  • I am sending attached file using above code.But , when click on send button it will open all installed mailing app like gmail,yahoo,Skype etc...but i want directly open the gmail.i don't need all unnecessary apps.But i am using first posted code it directly opens the gmail but i unable to attach the files. – suman May 20 '13 at 09:13
  • oh, let me revise the answer then..it was not clear from your quesiton.. – Dory Zidon May 20 '13 at 09:14
  • by using intent.setClassName("com.google.android.gm", "com.google.android.gm.ComposeActivityGmail"); this code it's worked fine in android version 4.1.1 but in 2.2 i didn't see any attached file in compose mail. – suman May 20 '13 at 09:27
  • ok, so you should try the second option. please note you will have to choose between the two options as one might not work, or raise an exception.. – Dory Zidon May 20 '13 at 10:12