user1667474
user1667474

Reputation: 819

Xamarin.Forms get image path

Using the Xamarin.Forms Camera sample from here - https://github.com/XForms/Xamarin-Forms-Labs-Samples/tree/master/XF.Labs.CameraSample i am able to select or take a photo. I then need to convert this image to binary

so here is the code that selects the image:

private async Task SelectPicture()
{
    mediaPicker = DependencyService.Get<IMediaPicker>();
    imageSource = null;
        var mediaFile = await mediaPicker.SelectPhotoAsync(new CameraMediaStorageOptions
            {
                DefaultCamera = CameraDevice.Front,
                MaxPixelDimension = 400
            });
        imageSource = ImageSource.FromStream(() => mediaFile.Source);
        img.Source  = imageSource;

}

and the code that takes the image is very similar

private async Task TakePicture()
{
    mediaPicker = DependencyService.Get<IMediaPicker>();
    imageSource = null;
        var mediaFile = await mediaPicker.TakePhotoAsync(new CameraMediaStorageOptions
            {
                DefaultCamera = CameraDevice.Front,
                MaxPixelDimension = 400
            });
        imageSource = ImageSource.FromStream(() => mediaFile.Source);
        img.Source  = imageSource;

}

How do i get the actual string imagePath from the above so that i can call the DependencyService below?

    public  byte[] ImageToBinary(string imagePath)
    {

        FileStream fileStream = new FileStream(imagePath, FileMode.Open, FileAccess.Read);
        byte[] buffer = new byte[fileStream.Length];
        fileStream.Read(buffer, 0, (int)fileStream.Length);
        fileStream.Close();
        return buffer;
    }

Upvotes: 1

Views: 6723

Answers (2)

Vinit Saxena
Vinit Saxena

Reputation: 11

mediaFile.Path will give you the actual path

Upvotes: 0

user1667474
user1667474

Reputation: 819

Shouldn't do this stuff tired

It is built in to the MediaFile so the answer is simply mediaFile.Path.ToString();

Upvotes: 3

Related Questions