Forum Xamarin.Forms

Camera problem in xamarin

Sometime i get error black screen ProjectName_Andrioid can log out when i take a picture with Camera using( CrossMedia.Current.TakePhotoAsync)
Is any way to solve.

Answers

  • LeonLuLeonLu Member, Xamarin Team Xamurai

    @TTW I wrote a demo about use this MediaPlugin, Here is running GIF.

    Using following code.

        async private void Button_Clicked(object sender, EventArgs e)
        {
            await CrossMedia.Current.Initialize();
    
            if (!CrossMedia.Current.IsCameraAvailable || !CrossMedia.Current.IsTakePhotoSupported)
            {
                DisplayAlert("No Camera", ":( No camera available.", "OK");
                return;
            }
    
            var file = await CrossMedia.Current.TakePhotoAsync(new Plugin.Media.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();
                return stream;
            });
          }
       }
    

    Here is layout.

        <StackLayout>
             <Button Text="click" Clicked="Button_Clicked"/>
            <Image x:Name="image"/>
        </StackLayout>
    

    Here is my demo.

Sign In or Register to comment.