예제 #1
0
        private async void CameraAction(object obj)
        {
            try
            {
                IsBusy = true;

                if (!CrossMedia.Current.IsCameraAvailable || !CrossMedia.Current.IsTakePhotoSupported)
                {
                    await MessageHelper.ErrorAsync(":( Permission not granted to photos.");

                    return;
                }

                var file = await CrossMedia.Current.TakePhotoAsync(new Plugin.Media.Abstractions.StoreCameraMediaOptions
                {
                    PhotoSize = Plugin.Media.Abstractions.PhotoSize.Medium,
                    Directory = "Pictures",
                    Name      = "image.jpg"
                });

                if (file == null)
                {
                    return;
                }
                var image = new ProductImage
                {
                    ProductId = Model.Id
                };
                var stream = file.GetStream();
                using (MemoryStream ms = new MemoryStream())
                {
                    stream.CopyTo(ms);
                    image.Buffer = ms.ToArray();
                }

                file.Dispose();

                image = await Products.AddPhoto(image);

                if (image != null)
                {
                    Model.ProductImage.Add(image);
                    Pictures.Add(new ProductImageModel(image));
                    await Toas.ShowLong("Success !");
                }
            }
            catch (Exception ex)
            {
                await MessageHelper.ErrorAsync(ex.Message);
            }
            finally
            {
                IsBusy = false;
            }
        }