/// <summary>
        /// Allows the user to pick an image to display for a question
        /// </summary>
        /// <param name="sender"></param>
        /// <returns></returns>
        private async Task PickImageAsync(object sender)
        {
            await CrossMedia.Current.Initialize();

            Plugin.Media.Abstractions.MediaFile file = await CrossMedia.Current.PickPhotoAsync();

            if (file != null) // if the user actually picked an image
            {
                MemoryStream memoryStream = new MemoryStream();
                file.GetStream().CopyTo(memoryStream);

                if (memoryStream.Length < 3000000)
                {
                    ImageButton currentImage;

                    currentImage = ((ImageButton)((StackLayout)((View)sender).Parent).Children[6]);

                    currentImage.Source = file.Path;

                    // Enables the image
                    currentImage.IsVisible = true;
                    if (sender is Button)
                    {
                        ((Button)sender).IsVisible = false;
                    }
                }
                else
                {
                    await this.DisplayAlert("Couldn't use Picture", "Pictures must be under 3 MB", "Back");
                }
                file.Dispose();
            }
        }
Beispiel #2
0
 private byte[] ConvertStreamToByteArray(Plugin.Media.Abstractions.MediaFile img)
 {
     using (var memoryStream = new MemoryStream())
     {
         img.GetStream().CopyTo(memoryStream);
         img.Dispose();
         return(memoryStream.ToArray());
     }
 }
 private byte[] ImageToBytes()
 {
     if (file == null)
     {
         return(null);
     }
     using (var memoryStream = new MemoryStream())
     {
         file.GetStream().CopyTo(memoryStream);
         file.Dispose();
         return(memoryStream.ToArray());
     }
 }
Beispiel #4
0
        private async void AddImage_Clicked(object sender, EventArgs e)
        {
            if (this.BugImage.IsEnabled)
            {
                switch (await this.DisplayActionSheet("Image Options", "Cancel", "Remove", "Change"))
                {
                case "Cancel":
                    return;

                case "Remove":
                    this.BugImage.IsEnabled      = false;
                    this.BugImageFrame.IsEnabled = false;
                    this.BugImageFrame.IsVisible = false;
                    this.ImagePath = null;
                    return;

                case "Change":
                default:
                    break;
                }
            }

            await CrossMedia.Current.Initialize();

            Plugin.Media.Abstractions.MediaFile file = await CrossMedia.Current.PickPhotoAsync();

            if (file != null) // if the user actually picked an image
            {
                MemoryStream memoryStream = new MemoryStream();
                file.GetStream().CopyTo(memoryStream);

                if (memoryStream.Length < 3000000)
                {
                    this.BugImage.IsEnabled      = true;
                    this.BugImageFrame.IsEnabled = true;
                    this.BugImageFrame.IsVisible = true;
                    this.BugImage.Source         = FileImageSource.FromFile(file.Path);
                    this.ImagePath = file.Path;
                }
                else
                {
                    await this.DisplayAlert("Couldn't use Picture", "Pictures must be under 3 MB", "Back");
                }
                file.Dispose();
            }
        }
        private async Task InsertFaultPicture(Plugin.Media.Abstractions.MediaFile picture)
        {
            byte[] pictureBytes;

            using (var memoryStream = new MemoryStream())
            {
                picture.GetStream().CopyTo(memoryStream);
                picture.Dispose();
                pictureBytes = memoryStream.ToArray();
            }

            var dbPicture = new FaultPicture
            {
                Picture = pictureBytes,
                FaultId = faultContext.FaultId
            };

            await DatabaseService.InsertFaultPicture(dbPicture);
        }
        public async void _bothButtons(bool isTakeClicked)
        {
            Plugin.Media.Abstractions.MediaFile file = null;
            //var file = (Plugin.Media.Abstractions.IMedia)null;
            await CrossMedia.Current.Initialize();

            if (!CrossMedia.Current.IsPickPhotoSupported || !CrossMedia.Current.IsCameraAvailable || !CrossMedia.Current.IsTakePhotoSupported)
            {
                if (isTakeClicked == false)
                {
                    Console.WriteLine("No Gallery", ":( No gallery available.", "OK");
                }
                else
                {
                    Console.WriteLine("No Camera", ":( No camera available.", "OK");
                }
                return;
            }

            if (isTakeClicked == true)
            {
                // Launch camera

                //progress.Indeterminate = true;
                //progress.SetCancelable(false);
                //progress.SetProgressStyle(Android.App.ProgressDialogStyle.Spinner);
                //progress.SetTitle("Please wait...");
                //progress.Show();


                file = await CrossMedia.Current.TakePhotoAsync(new Plugin.Media.Abstractions.StoreCameraMediaOptions
                {
                    Directory = "temp",
                    // CompressionQuality = 25,
                    SaveToAlbum = false
                });
            }
            else if (isTakeClicked == false)
            {
                // Choose from gallery

                file = await CrossMedia.Current.PickPhotoAsync(new Plugin.Media.Abstractions.PickMediaOptions
                {
                    // CompressionQuality = 25
                });
            }
            if (file == null)
            {
                //progress.Dismiss();
                return;
            }

            string filePath = file.Path;

            file.Dispose();

            var TakePhoto = new Android.Content.Intent(this, typeof(TakePhotoActivity));

            TakePhoto.PutExtra("imageLocation", filePath);
            StartActivity(TakePhoto);
        }
Beispiel #7
0
        /// <summary>
        /// Adds an icon from storage
        /// </summary>
        async void AddIconStored()
        {
            await Xamarin.Forms.Application.Current.MainPage.Navigation.PopAllPopupAsync();

            await Plugin.Media.CrossMedia.Current.Initialize();

            var status = await Plugin.Permissions.CrossPermissions.Current.CheckPermissionStatusAsync(Plugin.Permissions.Abstractions.Permission.Photos);

            if (status != Plugin.Permissions.Abstractions.PermissionStatus.Granted)
            {
                if (await Plugin.Permissions.CrossPermissions.Current.ShouldShowRequestPermissionRationaleAsync(Plugin.Permissions.Abstractions.Permission.Photos))
                {
                    await Acr.UserDialogs.UserDialogs.Instance.AlertAsync("Need access to photos to make icon");
                }

                var results = await Plugin.Permissions.CrossPermissions.Current.RequestPermissionsAsync(new[] { Plugin.Permissions.Abstractions.Permission.Photos });

                status = results[Plugin.Permissions.Abstractions.Permission.Photos];
            }

            if (status == Plugin.Permissions.Abstractions.PermissionStatus.Granted)
            {
                var userInput = await Acr.UserDialogs.UserDialogs.Instance.PromptAsync("Name the icon to add");

                if (userInput == null || string.IsNullOrWhiteSpace(userInput.Text))
                {
                    return;
                }

                Plugin.Media.Abstractions.MediaFile file = await Plugin.Media.CrossMedia.Current.PickPhotoAsync(new Plugin.Media.Abstractions.PickMediaOptions
                {
                    CustomPhotoSize    = 500,
                    CompressionQuality = 80,
                    RotateImage        = true,
                });

                if (file == null || file.Path == null)
                {
                    return;
                }

                if (System.IO.File.Exists(@file.Path))
                {
                    byte[] imageArray = null;

                    //if (Device.RuntimePlatform == Device.Android)
                    //{
                    //    imageArray = DependencyService.Get<InterfaceBitmapResize>().RotateImage(@file.Path);
                    //}
                    //else
                    //{
                    imageArray = System.IO.File.ReadAllBytes(@file.Path);
                    //}

                    string base64ImageRepresentation = System.Convert.ToBase64String(imageArray);

                    file.Dispose();

                    Storage.CommunicationIcon dynamicIcon = new Storage.CommunicationIcon()
                    {
                        Tag                  = IconRoles.GetRoleInt(IconRoles.Role.Communication),
                        Text                 = userInput.Text,
                        Local                = false,
                        IsStoredInFolder     = false,
                        FolderContainingIcon = "",
                        Base64               = base64ImageRepresentation,
                        Scale                = 1f,
                        X = -1,
                        Y = -1
                    };

                    IconImage testImage = null;

                    try
                    {
                        testImage = App.ImageBuilderInstance.BuildCommunicationIconDynamic(icon: dynamicIcon);
                    }
                    catch
                    {
                        return;
                    }

                    SaveCommunicationIconEvent(testImage);
                }
            }
            else if (status != Plugin.Permissions.Abstractions.PermissionStatus.Unknown)
            {
                await Acr.UserDialogs.UserDialogs.Instance.AlertAsync("Can not continue, try again");
            }
        }