public async void Show(Page page, string imageFile = null)
        {
            if (imageFile == null)
            {
                await CrossMedia.Current.Initialize();

                MediaFile file;

                string action = await page.DisplayActionSheet(SelectSourceTitle, CancelButtonTitle, null, TakePhotoTitle, PhotoLibraryTitle);

                if (action == TakePhotoTitle)
                {
                    if (!CrossMedia.Current.IsCameraAvailable || !CrossMedia.Current.IsTakePhotoSupported)
                    {
                        await page.DisplayAlert("No Camera", ":( No camera available.", "OK");

                        Faiure?.Invoke();
                        return;
                    }

                    file = await CrossMedia.Current.TakePhotoAsync(StoreCameraMediaOptions);
                }
                else if (action == PhotoLibraryTitle)
                {
                    if (!CrossMedia.Current.IsPickPhotoSupported)
                    {
                        await page.DisplayAlert("Error", "This device is not supported to pick photo.", "OK");

                        Faiure?.Invoke();
                        return;
                    }
                    file = await CrossMedia.Current.PickPhotoAsync(PickMediaOptions);
                }
                else
                {
                    Faiure?.Invoke();
                    return;
                }

                if (file == null)
                {
                    Faiure?.Invoke();
                    return;
                }

                imageFile = file.Path;
            }

            // small delay
            await Task.Delay(TimeSpan.FromMilliseconds(100));

            DependencyService.Get <IImageCropperWrapper>().ShowFromFile(this, imageFile);
        }
Пример #2
0
        public async void Show(Page page, string imageFile = null)
        {
            if (imageFile == null)
            {
                FileResult file    = null;
                string     newFile = null;

                string action = await page.DisplayActionSheet(SelectSourceTitle, CancelButtonTitle, null, TakePhotoTitle, PhotoLibraryTitle);

                try
                {
                    if (action == TakePhotoTitle)
                    {
                        file = await MediaPicker.CapturePhotoAsync(MediaPickerOptions);
                    }
                    else if (action == PhotoLibraryTitle)
                    {
                        file = await MediaPicker.PickPhotoAsync(MediaPickerOptions);
                    }
                    else
                    {
                        Faiure?.Invoke();
                        return;
                    }

                    //Si se capturo correctamente
                    if (file != null)
                    {
                        // save the file into local storage
                        newFile = Path.Combine(FileSystem.CacheDirectory, file.FileName);
                        //Copiarlo llevaba mucho trabajo

                        /*
                         * using (var stream = await file.OpenReadAsync())
                         * using (var newStream = File.OpenWrite(newFile)) {
                         *  await stream.CopyToAsync(newStream);
                         *  stream.Close();
                         *  newStream.Close();
                         * }
                         */
                        //Mover a cache local
                        if (File.Exists(newFile))
                        {
                            File.Delete(newFile);
                        }
                        File.Move(file.FullPath, newFile);
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine($"CapturePhotoAsync THREW: {ex.Message}");
                }

                if (file == null || newFile == null)
                {
                    Faiure?.Invoke();
                    return;
                }
                if (Device.RuntimePlatform == Device.Android)
                {
                    //Delay for fix Xamarin.Essentials.Platform.CurrentActivity no MediaPicker
                    await Task.Delay(TimeSpan.FromMilliseconds(2000));
                }
                imageFile = newFile;
            }

            // small delay
            await Task.Delay(TimeSpan.FromMilliseconds(100));

            DependencyService.Get <IImageCropperWrapper>().ShowFromFile(this, imageFile);
        }