3

I am trying to attach multiple images to an email.

I have tried the next code, but I don`t know what I am doing wrong.

I need to call the images by the Integer Array that you will see and attach them to an email.

Some of the class look like this:

Integer[] images = {
        R.drawable.image1,
        R.drawable.image2,
        R.drawable.image3,
        R.drawable.image4 };

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    switch(v.getId()){

    case R.id.bSendEmail:

        Intent emailintent2 = new Intent(android.content.Intent.ACTION_SEND_MULTIPLE);
        emailintent2.setType("plain/text");
        emailintent2.putExtra(Intent.EXTRA_EMAIL, emailaddress2);
        emailintent2.putExtra(Intent.EXTRA_SUBJECT, corsub);
        emailintent2.putExtra(Intent.EXTRA_TEXT, message2);

        ArrayList<Uri> uris = new ArrayList<Uri>();

        uris.add(Uri.parse("android.resource://" + getPackageName() + "/" + images[0]));
        uris.add(Uri.parse("android.resource://" + getPackageName() + "/" + images[1]));
        uris.add(Uri.parse("android.resource://" + getPackageName() + "/" + images[2]));
        uris.add(Uri.parse("android.resource://" + getPackageName() + "/" + images[3]));

        emailintent2.putExtra(Intent.EXTRA_STREAM, uris);
        startActivity(emailintent2);

     break;
Roberto Zuniga
  • 437
  • 1
  • 7
  • 16
  • I think @DjHacktorReborn has an answer. Another answer: http://stackoverflow.com/questions/2264622/android-multiple-email-attachments-using-intent – Lucy Jul 22 '13 at 18:42

2 Answers2

1

Bad news. It's simply not supported.

Have you thought of creating a ZIP archive of the attachments, and attach the archive?

(Note: Even that does not work good enough for me currently, but many seem to be able to live with it.)

class stacker
  • 5,357
  • 2
  • 32
  • 65
  • A ZIP its not good for me, I need to send the images to a person by email and I need that any person who receive the email get the images has attachments. – Roberto Zuniga Mar 06 '13 at 07:58
  • If your recipients cannot be bothered with opening a ZIP archive and if this is really important, I would send a correctly formatted multipart MIME message from code instead via the e-mail apps. The e-mail apps are a pain in the back when it comes to correctly setting MIME types for the body AND the attachment and handling attachments reasonably (don't you forget to delete them after a month or so!). Good luck. – class stacker Mar 12 '13 at 12:02
0

use

emailintent2.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);

instead of

emailintent2.putExtra(Intent.EXTRA_STREAM, uris);

using file

private String root = Environment.getExternalStorageDirectory().getPath()
            + Tags.DIRECTORY_PATH;

 path = new ArrayList<String>();

            File f = new File(root);

            File[] files = f.listFiles();

            if (!root.equals(root))

            {

                item.add(root);

                path.add(root);

                item.add("../");

                path.add(f.getParent());
            }
            for (int i = 0; i < files.length; i++)

            {

                File file = files[i];

                path.add(file.getPath());

                if (file.isDirectory())

                    item.add(file.getName() + "/");

                else

                    item.add(file.getName());


  ArrayList<Uri> uris = new ArrayList<Uri>();
            for (String file : path) {
                File fileIn = new File(file);
                Uri u = Uri.fromFile(fileIn);
                uris.add(u);
            }
DjHacktorReborn
  • 2,908
  • 2
  • 20
  • 29
  • It really work, but my images are send without ".png" extension, can you tell me how to add the extension? Thanks by the way. – Roberto Zuniga Mar 06 '13 at 08:06
  • @RobertoZuniga Please provide us with the information with which e-mail apps you can confirm this is working. SO is full of e-mail intent related "it works" announcements which work only with certain e-mail apps and/or versions. Thank you. – class stacker Mar 06 '13 at 08:16
  • Sorry, for a moment I believe it work, what is happening is that the images are sended without the ".png" and if I add it at the final it get an error when try to send the email. – Roberto Zuniga Mar 06 '13 at 08:16
  • you need to change type emailintent2.setType("plain/text"); to setType("image/*"); – DjHacktorReborn Mar 06 '13 at 08:31
  • I have do this: emailintent2.setType("image/*"); and also uris.add(Uri.parse("android.resource://" + getPackageName() + "/" + images[0]+".png")); but it did not work. – Roberto Zuniga Mar 06 '13 at 08:42
  • when the image get attached in the Gmail app, if I put the extension ".png" when I send shows an error in the Notification Bar, and if I don`t put the extension it sends the email but without the extension. – Roberto Zuniga Mar 06 '13 at 08:45
  • maybe using a File method, but I don`t have idea how to use it – Roberto Zuniga Mar 06 '13 at 08:51