Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 77050

PCL Xamarin Forms - Convert MediaFile into Byte Array

$
0
0

Hi, I'm trying to save image into Azure database. But i'm facing problem to convert MediaFile into Byte[]. I'm using Xam.Plugin.Medie ( http://www.nuget.org/packages/Xam.Plugin.Media ) to access camera and gallery. Please kindly guide me how to convert MedieFile into Byte[]. Thank you.

Image image = new Image();
    Button test = new Button
    {
            Text = "Take Photo"
    };
    Button test2 = new Button
    {
            Text = "Pick Photo"
    };

        Content = new StackLayout
        {
            Children = {
                test, test2, image
            }
        };

        test.Clicked += async (sender, args) =>
        {

            if (!CrossMedia.Current.IsCameraAvailable || !CrossMedia.Current.IsTakePhotoSupported)
            {
                await DisplayAlert("No Camera", ":( No camera avaialble.", "OK");
                return;
            }

            var file = await CrossMedia.Current.TakePhotoAsync(new Media.Plugin.Abstractions.StoreCameraMediaOptions
            {

                Directory = "Sample",
                Name = "test.jpg"
            });

            if (file == null)
                return;

            await DisplayAlert("File Location", file.Path, "OK");

            image.Source = ImageSource.FromStream(() =>
            {
                var stream = file.GetStream();
                file.Dispose();
                return stream;
            });
        };

        test2.Clicked += async (sender, args) =>
        {
            if (!CrossMedia.Current.IsPickPhotoSupported)
            {
                await DisplayAlert("Photos Not Supported", ":( Permission not granted to photos.", "OK");
                return;
            }
            var file = await CrossMedia.Current.PickPhotoAsync();


            if (file == null)
                return;

            image.Source = ImageSource.FromStream(() =>
            {
                var stream = file.GetStream();
                file.Dispose();
                return stream;
            });
        };

Viewing all articles
Browse latest Browse all 77050

Trending Articles