1

I've been working on Android program to send email with an attachment (text/plain)using Intent with Intent.ACTION_SEND I used Intent.putParcelableArrayListExtra(android.content.Intent.EXTRA_STREAM, uri) However, when I tried to have multiple files attached to the same mail by calling Intent.putExtra(android.content.Intent.EXTRA_STREAM, uri) multiple times, it failed to work. None of the attachment show up in the email.Thanks in Advance

      final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
      System.out.println(emailText+emailTo);
      emailIntent.setType("text/plain");
      emailIntent.putExtra(android.content.Intent.EXTRA_TEXT,emailText);
      emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject);
      emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL,new String[]{emailTo});

      //has to be an ArrayList
      ArrayList<Uri> uris = new ArrayList<Uri>();

      //convert from paths to Android friendly Parcelable Uri's
      try
      {
            for (String file : filePaths)
            {
                File fileIn = new File(context.getFilesDir(),file);
                System.out.println(fileIn+"yes");
                Uri u =  Uri.fromFile(fileIn);
                uris.add(u);
                System.out.println(u);
            }
      emailIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM,uris);
      context.startActivity(Intent.createChooser(emailIntent, "Send mail..."));
      }
Praveenkumar
  • 24,084
  • 23
  • 95
  • 173
ManjotSingh
  • 713
  • 7
  • 20

2 Answers2

2

Use the ACTION_SEND_MUTIPLE instead of ACTION_SEND

http://developer.android.com/reference/android/content/Intent.html#ACTION_SEND_MULTIPLE

final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND_MULTIPLE);
Priebe
  • 4,942
  • 3
  • 24
  • 38
  • thanks, I did it but got the same problem,i wasted my 2 days on a single issue.please help if u can – ManjotSingh May 10 '12 at 13:16
  • Which email app do you use? I get a error when using the standard "Email" app "file too large to attach". But I don't get the warning when using Gmail. Try using another mail app and see if it makes a difference. – Priebe May 14 '12 at 08:13
  • I was use both Gmail as well as yahoo too,but i didn't receive any attachment in both app,i dont know where i m doing wrong. – ManjotSingh May 16 '12 at 07:51
  • Do you get the same result if you run it in the emulator? – Priebe May 16 '12 at 09:14
0

Ensure that the second argument file supplies only a valid file name. your problem might be there...

File fileIn = new File(context.getFilesDir(),file);

The Following code works without any issue..

Intent emailIntent = new Intent(Intent.ACTION_SEND_MULTIPLE);
//...
ArrayList<Uri> uris = new ArrayList<Uri>();
uris.add(Uri.parse("file://" + Environment.getExternalStorageDirectory().getPath() + "/HOME/Pictures/1.png"));
uris.add(Uri.parse("file://" + Environment.getExternalStorageDirectory().getPath() + "/HOME/Pictures/2.png"));
uris.add(Uri.parse("file://" + Environment.getExternalStorageDirectory().getPath() + "/HOME/Pictures/3.png"));
uris.add(Uri.parse("file://" + Environment.getExternalStorageDirectory().getPath() + "/HOME/Pictures/4.png"));
emailIntent.putParcelableArrayListExtra(android.content.Intent.EXTRA_STREAM, uris);
startActivity(emailIntent);
mifthi
  • 151
  • 8