简体   繁体   中英

Xamarin, Plugin.FilePicker -> Exception: "...View=DecorView

In my Xamarin project I use Plugin.FilePicker, everything works except one exception.

If I perform the action of selecting a file from 2 to 6 times, and the same number of times I click the button back I get an exception:

Unhandled Exception:

Java.Lang.IllegalArgumentException: View=DecorView@b10016d[] not attached to window manager

The class in which the plugin is used is made up entirely of try, catch blocks. In another class where I use the methods of the previous class I also equipped with try, catch blocks - for checking only. It does not catch an exception anywhere.

The project is written on Android and UWP. It occurs on the android, but on the UWP no.

Any ideas?

EDIT

Thanks for answer :) This is part of my code:

public  class FilePickerService
    {
        private FileData fileData;
        public FilePickerService()
        {
            fileData = new FileData();
        }

        public async Task<bool> pickFile()
        {
            try
            {
                fileData = await CrossFilePicker.Current.PickFile();
                if (fileData == null)
                    return false; // user canceled file picking
                else
                {
                    string fileName = fileData.FileName;
                    string contents = System.Text.Encoding.UTF8.GetString(fileData.DataArray);

                    System.Console.WriteLine("File name chosen: " + fileName);
                    System.Console.WriteLine("File data: " + contents);

                    return true;
                }

            }
            catch (Exception ex)
            {
                System.Console.WriteLine("Exception choosing file: " + ex.ToString());
                return false;
            }
        }
        public FileData getFileData()
        {
            try
            {
                if (fileData != null)
                {
                    return fileData;
                }
                else
                {
                    return null;
                }
            }
            catch(Exception ex)
            {
                var error = ex.Message;
                return null;
            }

        }
        public string getFileName()
        {
            try
            {
                string fileName = fileData.FileName;
                return fileName;
            }
            catch(Exception ex)
            {
                var error = ex.Message;
                return "";
            }


        }

        public string getFileContents()
        {
            try
            {
                string contents = System.Text.Encoding.UTF8.GetString(fileData.DataArray);
                return contents;
            }
            catch (Exception ex)
            {
                var error = ex.Message;
                return "";
            }

        }

        public bool fileIsPicture()
        {
            try
            {
                string fileName = fileData.FileName;
                bool isJPG = false;
                bool isPNG = false;

                isJPG = Path.GetExtension(fileName).Equals(".jpg", StringComparison.InvariantCultureIgnoreCase) || Path.GetExtension(fileName).Equals(".jpeg", StringComparison.InvariantCultureIgnoreCase);
                isPNG = Path.GetExtension(fileName).Equals(".png", StringComparison.InvariantCultureIgnoreCase);

                if (isJPG == true || isPNG == true)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch(Exception ex)
            {
                var error = ex.Message;
                return false;
            }
        }

        public bool fileIsExist()
        {
            try
            {
                if (fileData != null)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch(Exception ex)
            {
                var error = ex.Message;
                return false;
            }

        }
    }

.. A similar code can be selected for 3 pictures. Below I presented support for 1 out of 3.

    public Image imagePhotoOne;
    public Image imageTakePhotoOne;
    private Service.FilePickerService serviceImagePickerOne;

//Another code...

    imagePhotoOne = new Image();
    imageTakePhotoOne = new Image();
    serviceImagePickerOne = new Service.FilePickerService();

//Another code...

     TapGestureRecognizer tapGestureImageTakePhotoOne = new TapGestureRecognizer();
                tapGestureImageTakePhotoOne.Tapped += async (sender, e) =>
                {
                    try
                    {
                        var status = await serviceImagePickerOne.pickFile();

                        if (status == true)
                        {
                            if (serviceImagePickerOne.fileIsPicture() == true)
                            {
                                imagePhotoOne.IsVisible = true;
                                imageTakePhotoOne.IsVisible = false;
                                imagePhotoOne.Source = ImageSource.FromStream(() =>
                                {
                                    return new MemoryStream(serviceImagePickerOne.getFileData().DataArray);
                                });

                            }
                            else
                            {
                                await Task.Delay(100);
                                UserDialogs.Instance.Toast("Zdjecie nie jest w formacje .jpg/.jpeg/.png");
                                await Task.Delay(100);
                                imagePhotoOne.IsVisible = false;
                                imageTakePhotoOne.IsVisible = true;
                                imagePhotoOne.Source = null;
                            }
                        }
                        else
                        {
                            await Task.Delay(100);
                            UserDialogs.Instance.Toast("Nie wybrano pliku");
                            await Task.Delay(100);
                        }
                    }
                    catch(Exception ex)
                    {
                        var error = ex.Message;
                    }

                };
                imageTakePhotoOne.GestureRecognizers.Add(tapGestureImageTakePhotoOne);

A similar code can be used to select 3 files. Below I presented support for 1 out of 3.

public Label labelFileOne;
public Image imageTakeFileOne;
private Service.FilePickerService serviceFilePickerOne;

//Code create elements

 TapGestureRecognizer tapGestureImageTakeFileOne = new TapGestureRecognizer();
            tapGestureImageTakeFileOne.Tapped += async (sender, e) =>
            {
                try
                {
                    var status = await serviceFilePickerOne.pickFile();

                    if (status == true)
                    {
                        if (serviceFilePickerOne.getFileData() != null)
                        {
                            labelFileOne.IsVisible = true;
                            labelFileOne.Text = serviceFilePickerOne.getFileName();
                            imageTakeFileOne.IsVisible = false;
                        }
                        else
                        {
                            await Task.Delay(100);
                            UserDialogs.Instance.Toast("Nie udało się wczytać pliku");
                            await Task.Delay(100);
                            labelFileOne.IsVisible = false;
                            labelFileOne.Text = "";
                            imageTakeFileOne.IsVisible = false;
                        }
                    }
                    else
                    {
                        await Task.Delay(100);
                        UserDialogs.Instance.Toast("Nie wybrano pliku");
                        await Task.Delay(100);
                    }
                }
               catch(Exception ex)
                {
                    var error = ex.Message;
                }


            };
            imageTakeFileOne.GestureRecognizers.Add(tapGestureImageTakeFileOne);

The error occurs when I use Plugin.FilePicker 3 to 6 times, but I do not choose any file every time.

Answer Edited Looks like you need media picker as you're choosing only png and jpeg files. I reproduced the issue with the plugin you're using. Please use xam.plugin.media instead, it should fix your issue.

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