简体   繁体   中英

Compulsary requirement to take and store users photo using Xam.Plugin.Media

I have a PCL type app and I'm using the Xam.Plugin.Media plugin. I need it to ensure a user submits a photo from the camera before they can continue.

To do this I show the camera page from a button click event and I want to ensure that in case the user cancels out of this that the app launches the camera again, this would repeat until a photograph is stored.

Currenty my app falls in the onActivityResumed method of the MainApplication file when the user cancels out of the camera

Attached photo of my code, My code .

 private async void TakePicture()
        {
            await CrossMedia.Current.Initialize();

            if (!CrossMedia.Current.IsCameraAvailable || !CrossMedia.Current.IsTakePhotoSupported)
            {
                await App.Current.MainPage.DisplayAlert("No Camera", ":( No camera available.", "Aceptar");
            }



            file = await CrossMedia.Current.TakePhotoAsync(new Plugin.Media.Abstractions.StoreCameraMediaOptions
            {
                Directory = "Sample",
                Name = "test.jpg",
                PhotoSize = PhotoSize.Small,
            });

            //IsRunning = true;


            if (file != null)
            {
                ImageSource = ImageSource.FromStream(() =>
                {
                    var stream = file.GetStream();
                    return stream;
                });
            }

            IsRunning = false;
        }

Aside from the fact that it is usually a bit of a UX issue to force a user into anything nowadays, the question still has some merits.

this is the approach that I would consider, it involves recursion.

    private async void TakePicture()
    {
        await CrossMedia.Current.Initialize();

        if (!CrossMedia.Current.IsCameraAvailable || !CrossMedia.Current.IsTakePhotoSupported)
        {
            await App.Current.MainPage.DisplayAlert("No Camera", ":( No camera available.", "Aceptar");
        }

        file = await CrossMedia.Current.TakePhotoAsync(new Plugin.Media.Abstractions.StoreCameraMediaOptions
        {
            Directory = "Sample",
            Name = "test.jpg",
            PhotoSize = PhotoSize.Small,
        });

        //IsRunning = true;

        if (file != null)
        {
            ImageSource = ImageSource.FromStream(() =>
            {
                var stream = file.GetStream();
                return stream;
            });
        }
        else
        {
            // Recursion - I believe that this would continue until the file is not null, then it would carry on.
            TakePicture();
        }

        IsRunning = false;
    }

I can't say I use recursion that often, but I think it might do the trick here.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM