title: Android how to attach an image byte[] to an email as an attachment?
body: <p>I'm writing this in Xamarin but the answer can be for Android Java (and I'll translate) or Xamarain.</p>
  
  <p>I have a c# byte array (named attachment) of the contents of an gif like image so I cannot turn it into an image object as the contents inside would be lost. Think of it as a byte array of a file. I want to attach this image to an email. I have the following code.</p>
  
  <pre><code>//Create the email intent.
  Intent emailIntent = new Intent(Intent.ActionSend);
  
  //Add a flag to let the system know it can start this activity.
  emailIntent.AddFlags(ActivityFlags.NewTask);
  
  //Put the subject in the email.
  emailIntent.PutExtra(Intent.ExtraSubject, subject);
  
  //Put the body in the email.
  emailIntent.PutExtra(Intent.ExtraText, body);
  
  //Add the attachment.
  emailIntent.PutExtra(Intent.ExtraStream, attachment);
  
  //Set the intent type to the intent type of the email.
  emailIntent.SetType("message/rfc822");
  
  //Create a chooser intent to ask the user to choose the application for email.
  Intent finalIntent = Intent.CreateChooser(emailIntent, "Send Email");
  </code></pre>
  
  <p>When I hit the code, it asks for an email program, I chose GMail, and I see my subject and body but no attachment.</p>
  
  <p>How can I attach the byte array to the email to get it to attach?</p>
  
  <p><strong>Edit</strong>
  This question was marked as a duplicate. Here is why this question is NOT the same as <a href="https://stackoverflow.com/questions/17160593/how-to-attach-a-bitmap-when-launching-action-send-intent">How to attach a Bitmap when launching ACTION_SEND intent</a></p>
  
  <p>That question:
  1. Involves sending a bitmap. I don't have a bitmap but rather a in memory byte array. As mentioned, I CANNOT turn this byte array into a bitmap image as the contents inside will become compressed and modified. Even if I could get the bitmap to not be modified, every answer in that question runs a .compress on the bitmap.
  2. This byte array should not be saved to the device for security. I generate the byte array and send it off. I would need a way to attach it in memory and not saving it to the file system.</p>