SilentCoder
SilentCoder

Reputation: 2000

Exception throw out when trying to open the camera using Xam.Plugin.Media

When I'm trying to open the camera using await CrossMedia.Current.TakePhotAsync() Im getting below exception. It doesn't open any camera in the application.

Field overflow on store InterpretedSystem.Runtime.CompilerServices.IAsyncStateMachineVT.<>7__wrap2

My code as below,

        public UserProfilePage()
        {
            InitializeComponent();
           
            CameraButton.Clicked += CameraButton_Clicked;
            BindingContext = new UserProfileViewModel(this.Navigation);
        }  

        private async void CameraButton_Clicked(object sender, EventArgs e)
        {
            if (CrossMedia.Current.IsCameraAvailable && CrossMedia.Current.IsTakePhotoSupported)
            {
               
                // Supply media options for saving our photo after it's taken.
                var mediaOptions = new Plugin.Media.Abstractions.StoreCameraMediaOptions
                {
                    Directory = "Receipts",
                    Name = $"{DateTime.UtcNow}.jpg"
                };

                // Take a photo of the business receipt.
                try
                {
                    var file = await CrossMedia.Current.TakePhotoAsync(mediaOptions);

                }
                catch (Exception exception)
                {
                    Console.WriteLine(exception);
                    throw;
                }
            }
        }

Xaml page like below,

<Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*" />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>

        <Image x:Name="PhotoImage" />
        <Button x:Name="CameraButton" Text="Take Photo" Grid.Row="1" />
    </Grid>

How to get my camera open and take a photo in xamarin. Below are the confugarations,

Xam.Plugin.Media = v3.1.2

Xamarin.Forms = v2.5.0.121934

VS2017

Upvotes: 0

Views: 769

Answers (2)

SilentCoder
SilentCoder

Reputation: 2000

This error occurs when you're trying to run Audio, Camera kind of features in the Xamarin Live Player.

Plugging actual physical device for debugging or release will solve the problem.

Upvotes: 1

Martin Zikmund
Martin Zikmund

Reputation: 39082

I think the reason could be in the target file name of the photo:

Name = $"{DateTime.UtcNow}.jpg"

The date contains characters that are invalid in a file path and that causes the exception.

You can use an alternative format that is suitable for file names:

Name = $"{DateTime.UtcNow.ToString("yyyy-dd-M--HH-mm-ss")}.jpg"

Upvotes: 0

Related Questions