0

I am trying to send an email via ACTION_SEND from my database in my app. All is working and all fields with user details are formatted correctly in the email body using html. However when i need to send an Image as well (using standard built in email client on android) i get a toast showing "Unable to attach file"

I have search and tried various solutions from SO but none of them helped me.

So this is what i have:

 Intent messageIntent = new Intent(android.content.Intent.ACTION_SEND);

    String aEmailList[] = {"user@example.com", "user1@example.com"};
    messageIntent.putExtra(android.content.Intent.EXTRA_EMAIL, aEmailList);
    messageIntent.putExtra(Intent.EXTRA_CC, new String[]{"user2@example.com", "user3@example.com"});
    messageIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject);

    messageIntent.setType("text/html");
    messageIntent.setType("image/png");
    messageIntent.putExtra(android.content.Intent.EXTRA_TEXT, message);
    Log.v(getClass().getSimpleName(), "simageUri=" + Uri.parse("file://"+ image));
    messageIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://"+ image));
    startActivity(messageIntent);
}

Now image is where the file is saved in my database. I have tried image.getAbsolutePath but this crashes with a null pointer exception

In my String.xml I have to body of the email.

<string
    name="feedbackmessagebody_format">
    <![CDATA[
    <html>
    <head>
   <style>
   table {
  border-collapse: collapse;
   }

    td, th {
    border: 1px solid black;
    padding: 3px;
    }
   </style>
    </head>
   <body>
   <table>
   <col width="300">
  <tr>
  <th>Field</th>
  <th>Description</th>
  </tr>
  <tr>
  <td><b>Image: </b></td>
  <td>%22$s</td>
  </tr>
  </table>

  </body>
  </html>
   ]]></string>

The %22$s is the number of fields i have in my email body and this is pointing to

  final ImageView feedbackField19 = (ImageView) findViewById(R.id.imageA);
  String feedback22 = feedbackField19.getDrawable().toString();

Not sure what i am missing here?

  • "I have tried image.getAbsolutePath but this crashes with a null pointer exception" -- then `image` is null, and `"file://"+ image` will be `file://null`, and that is not a valid filesystem path to your image. Also note that you are calling `setType()` twice (only the second will be honored), and that there is no requirement for apps to honor both `EXTRA_TEXT` and `EXTRA_STREAM`. – CommonsWare Jun 17 '17 at 17:13
  • @CommonsWare Thanks. I got it working, but only as attachment, But would really like to display the image in the email body instead –  Jun 17 '17 at 17:30
  • Upload the image to a Web server. Then, use the URL for that image. – CommonsWare Jun 17 '17 at 17:35
  • @CommonsWare, Thank, but i don't have a Webserver, but will make a plan. Thanks –  Jun 17 '17 at 17:42

1 Answers1

0
    String recepientEmail = ""; // either set to destination email or leave empty
    Intent intent = new Intent(Intent.ACTION_SENDTO);
    intent.setData(Uri.parse("mailto:" + recepientEmail));
    intent.putExtra(Intent.EXTRA_SUBJECT,"email subject");
    intent.putExtra(Intent.EXTRA_TEXT,message);
    intent.putExtra(Intent.EXTRA_STREAM,filePath);
    startActivity(intent);
Zahirul Islam
  • 155
  • 2
  • 12
  • you can use easyimage library for image & it will send you a file path . Please see details [link] (https://github.com/jkwiecien/EasyImage) – Zahirul Islam Jun 17 '17 at 16:44
  • Just a quick question? Is there a way of instead of attaching the image to display the image in the email body instead? –  Jun 17 '17 at 17:13
  • please see this answer https://stackoverflow.com/questions/14457457/android-intent-send-an-email-with-image-attachment – Zahirul Islam Jun 17 '17 at 17:20