示例#1
0
        private async void CameraCommandExecuted()
        {
            try
            {
                string action = await App.Current.MainPage.DisplayActionSheet("Seleccionar imagen", "Cancelar", null, "Galeria", "Camara");

                if (action == "Galeria")
                {
                    var statusStorage = await Utils.PermissionsAsync(Plugin.Permissions.Abstractions.Permission.Storage);

                    var statusPhoto = await Utils.PermissionsAsync(Plugin.Permissions.Abstractions.Permission.Photos);

                    if (statusPhoto && statusStorage)
                    {
                        if (!CrossMedia.Current.IsPickPhotoSupported)
                        {
                            //DependencyService.Get<IDialogs>().ToastMessage("Denegado permiso para la galeria");
                        }
                        var file = await Plugin.Media.CrossMedia.Current.PickPhotoAsync(new Plugin.Media.Abstractions.PickMediaOptions
                        {
                            PhotoSize = Plugin.Media.Abstractions.PhotoSize.Medium,
                        });

                        if (file == null)
                        {
                            return;
                        }
                        var galeryPhotoStream = file.GetStream();
                        file.Dispose();
                        Image = ConvertImage.ConvertImageStreamTobyte(galeryPhotoStream);
                    }
                }
                else if (action == "Camara")
                {
                    var statusCamera = await Utils.PermissionsAsync(Plugin.Permissions.Abstractions.Permission.Camera);

                    if (statusCamera)
                    {
                        await CrossMedia.Current.Initialize();

                        if (!CrossMedia.Current.IsCameraAvailable || !CrossMedia.Current.IsTakePhotoSupported)
                        {
                            //DependencyService.Get<IDialogs>().ToastMessage("no hay soporte de camara");
                            //await DisplayAlert("No Camera", ":( No camera available.", "OK");
                            return;
                        }
                        var file = await CrossMedia.Current.TakePhotoAsync(new Plugin.Media.Abstractions.StoreCameraMediaOptions
                        {
                            Directory              = "Sample",
                            Name                   = "test.jpg",
                            SaveToAlbum            = true,
                            CompressionQuality     = 75,
                            CustomPhotoSize        = 50,
                            PhotoSize              = Plugin.Media.Abstractions.PhotoSize.MaxWidthHeight,
                            MaxWidthHeight         = 2000,
                            DefaultCamera          = Plugin.Media.Abstractions.CameraDevice.Front,
                            ModalPresentationStyle = Plugin.Media.Abstractions.MediaPickerModalPresentationStyle.FullScreen,
                            RotateImage            = false
                        });

                        if (file == null)
                        {
                            return;
                        }
                        //await DisplayAlert("File Location", file.Path, "OK");
                        var stream = file.GetStream();
                        Image = ConvertImage.ConvertImageStreamTobyte(stream);
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }