views:

1628

answers:

8

Hi, I've been working on Android program to send email with attachment (image file , audio file , etc) using Intent with (ACTION_SEND) . THe program is working when email has a single attachment. I used Intent.putExtra( android.content.Intent.EXTRA_STREAM, uri) to attach the designated image file to the mail and it is wokring fine , the mail can be delivered through Gmail . However, when I tried to have multiple images 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 . I searched the SDK documentation and Android programming user group about email attachment but cannot find any related info. However, I've discovered that there's another intent constnat ACTION_SEND_MULTIPLE (available since API level 4 ) which might meet my requirement. Based on SDK documentation, it simply states that it deliver multiple data to someone else , it works like ACTION_SEND , excpet the data is multiple. But I still could not figure out the correct usage for this command. I tried to delcare intent with ACTION_SEND_MULTIPLE , then call putExtra(EXTRA_STREAM, uri) multiple times to attach multiple images , but I got the same errnoenous result just like before, none of the attachment show up in the email. Has anyone tried with ACTION_SEND_MULTIPLE and got it working with multiple email attachment ? Thanks very much for your help.

Regards

A: 

I'm trying to do exactly the same thing, with similar results to you.

The only difference is that when I tried calling Intent.putExtra(Intent.EXTRA_STREAM, uri) multiple times, it does attach and send the file associated with the final call. I'm therefore assuming that the last call to set this data overwrites what has been set by any previous calls.

I wonder if it's possible to construct a url that points to all the files that need to be attached, and make a single call to Intent.putExtra(Intent.EXTRA_STREAM, uri).

Sorry to not be more help. I'll post a proper answer if I get one.

Thanks Peter

Peter A
+1  A: 

Here I found great example http://blog.blackmoonit.com/2010/02/filebrowser-send-receive-intents.html

you must use

aIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM,theUris);
aIntent.setType(theOverallMIMEtype);
russenreaktor
+1  A: 

Here is the code you need to create an emailIntent that contains multiple attachments.

public static void email(Context context, String emailTo, String emailCC,
    String subject, String emailText, List<String> filePaths)
{
    //need to "send multiple" to get more than one attachment
    final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND_MULTIPLE);
    emailIntent.setType("plain/text");
    emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, 
        new String[]{emailTo});
    emailIntent.putExtra(android.content.Intent.EXTRA_CC, 
        new String[]{emailCC});
    //has to be an ArrayList
    ArrayList<Uri> uris = new ArrayList<Uri>();
    //convert from paths to Android friendly Parcelable Uri's
    for (String file : filePaths)
    {
        File fileIn = new File(file);
        Uri u = Uri.fromFile(fileIn);
        uris.add(u);
    }
    emailIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
    context.startActivity(Intent.createChooser(emailIntent, "Send mail..."));
}
gregm
A: 

ClassCastException... putParcelableArrayListExtra requires a parcelable not an ArrayList. Is this really so difficult? Do I have to write a new class implementing parcelable just to send some uris?

A: 

ClassCastException? Are you using the correct ArrayList<? implements Parcelable>? ArrayList<Uri> is acceptable for .putParcelableArrayListExtra() since Uri implements Parcelable. ArrayList<String> is not since String is not Parcelable -- for ArrayList<String> you need to use .putStringArrayListExtra(), but that should not be used in this case since you need an ArrayList of Uris, not Strings. The parameter does not require the ArrayList collection itself to be Parcelable, but the thing which it contains.

Uncle Code Monkey
A: 

I was sure this would work. But it seems that I'm still having only one attachment show up.

I'm using an Xperia X10 for development, so unfortunately I'm saddled with Android 1.6. Does anyone know if there is some kind of limitation in 1.6 with regards to number of email attachments?

TylerJames
A: 

you dont need a parseable list the uri array list works fine, I use something like this all the time

ArrayList<Uri> uris = new ArrayList<Uri>();
//convert from paths to Android friendly Parcelable Uri's
for (String file : filePaths)
{
    File fileIn = new File(file);
    Uri u = Uri.fromFile(fileIn);
    uris.add(u);
}

is exactly what you need

Shawn
A: 

I tried using the following code but no file is attaching...any ideas

ArrayList<Uri> uris = new ArrayList<Uri>();
//convert from paths to Android friendly Parcelable Uri's
for (String file : filePaths)
{
    File fileIn = new File(file);
    Uri u = Uri.fromFile(fileIn);
    uris.add(u);
}
emailIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
context.startActivity(Intent.createChooser(emailIntent, "Send mail..."));
AliDeo