1

I'm writing this in Xamarin but the answer can be for Android Java (and I'll translate) or Xamarain.

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.

//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");

When I hit the code, it asks for an email program, I chose GMail, and I see my subject and body but no attachment.

How can I attach the byte array to the email to get it to attach?

Edit This question was marked as a duplicate. Here is why this question is NOT the same as How to attach a Bitmap when launching ACTION_SEND intent

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.

Community
  • 1
  • 1
SolidSnake4444
  • 3,372
  • 6
  • 40
  • 63
  • @Jason I edited the question to explain why it is not a duplicate. – SolidSnake4444 Apr 22 '17 at 20:32
  • as best as I can tell, you have to save it to a file in order to send it as an attachment. You can save the data to file without converting it to a bitmap. If security is a concern, delete the file after you send it. – Jason Apr 22 '17 at 20:49
  • I see no byte array. Is 'attachment' a stream? – greenapps Apr 23 '17 at 08:28
  • Please show how you would attach a file. – greenapps Apr 23 '17 at 08:33
  • Which name should the email app use for the bytes? The receiver wants to see a file name i think. Or how would the email app or the receiving email app know that it is an image? – greenapps Apr 23 '17 at 08:34
  • Save the bytes of the array to private internal memory of your app. There no other app has access. Use a file provider to serve the file to the email app. Delete the file the next time you are going to send a file. – greenapps Apr 23 '17 at 08:37
  • The email app expects a file or an uri for the reasons i mentioned. – greenapps Apr 23 '17 at 08:40
  • @greenapps The "attachment" variable is a byte[] of the image. – SolidSnake4444 Apr 24 '17 at 01:24

0 Answers0