For a PCL application I'm developing I'm using the media plugin for Xamarin Forms by jamesmontemagno (Github Repo). I am mainly using the plugin for capturing photos through the device's camera, for this I'm using the TakeVideoAsync method (Task<MediaFile> TakeVideoAsync(StoreVideoOptions options);
). It returns a [MediaFile][2]
object.
The issue I'm facing is that I need to send the image as an email attachment. For this I will implement an "EmailSender" class which will have different implementations for every target platforms (I'm using the PCL approach). Right now I'm working on a Windows Phone 8.1 (Silverlight) implementation. I would like to use Windows.ApplicationModel.Email.EmailManager
to send a Windows.ApplicationModel.Email.EmailMessage
. An example implementation can be seen in this Stackoverflow answer (Look at the "ComposeEmail" method).
What I need help with:
I want to find a way of retrieving the image file (perhaps either as an StorageFile or RandomAccessStream) in order to make an EmailAttachment object from it. From the MediaFile
returned by the TakeVideoAsync
method in the media plugin I can get a System.IO.Stream
, but I haven't found any way of converting it to a RandomAccessStream
with the functionality of the .NET version my Xamarin Windows Phone 8.1
All suggestions will be very appreciated.
@jimutt - Windows Phone 8.1 Silverlight is now officially deprecated in Xamarin Forms and I suggest you move over to Windows Phone 8.1 Runtime or UWP (if you can) as soon as possible.
Then to save it to a file you might want to look at Working with Files and pass through a byte[].
To get the stream into a byte[] using a MemoryStream makes this quite easy
using (var memoryStream = new MemoryStream()) { imageStream.CopyTo(memoryStream); return memoryStream.ToArray(); }
Answers
@jimutt - Windows Phone 8.1 Silverlight is now officially deprecated in Xamarin Forms and I suggest you move over to Windows Phone 8.1 Runtime or UWP (if you can) as soon as possible.
Then to save it to a file you might want to look at Working with Files and pass through a byte[].
To get the stream into a byte[] using a MemoryStream makes this quite easy
@AdamP Thank you, I've got a working solution now. Yes, I am aware of the deprecation, I'm only using Silverlight due to some compatibility requirements of some other plugins which I were using earlier. I might look into moving over to WP RT instead in the future though.