Search code examples
xamarinfilepickerxamarin.essentials

Why the application closes when using xamarin essentials filepicker?


My application closes without throwing any exceptions when I try to use Xamarin.Essentials' FilePicker class.

What did I do:

added: <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

And the code:

async Task<FileResult> PickAndShow(PickOptions options)
{
    try
    {
        var result = await FilePicker.PickAsync();
        if (result != null)
        {
            Text = $"File Name: {result.FileName}";
            if (result.FileName.EndsWith("jpg", StringComparison.OrdinalIgnoreCase) ||
                result.FileName.EndsWith("png", StringComparison.OrdinalIgnoreCase))
            {
                var stream = await result.OpenReadAsync();
                Image = ImageSource.FromStream(() => stream);
            }
        }
    }
    catch (Exception ex)
    {
        // The user canceled or something went wrong
    }
}

I am using VS for MacOS.

Video of closing the app: https://drive.google.com/file/d/1H1488amryi_p7mkg7JdmhcGAE569KON4/view?usp=sharing

Please help me to see what I'm missing?

Edit:

line that the error occurs var result = await FilePicker.PickAsync();

Exception:

No exception is thrown;

application exit:

[Mono] Requesting loading reference 5 (of 8) of Xamarin.Essentials.dll [Mono] Loading reference 5 of Xamarin.Essentials.dll asmctx DEFAULT, looking for System.Core, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e [Mono] Assembly Ref addref Xamarin.Essentials[0x7ddc93de80] -> System.Core[0x7d850f7880]: 10 [Choreographer] Skipped 711 frames! The application may be doing too much work on its main thread. [OpenGLRenderer] Davey! duration=12935ms; Flags=0, IntendedVsync=24578200518089, Vsync=24591127790687, OldestInputEvent=9223372036854775807, NewestInputEvent=0, HandleInputStart=24591131789208, AnimationStart=24591131799939, PerformTraversalsStart=24591132369131, DrawStart=24591132532323, SyncQueued=24591132572054, SyncStart=24591133134246, IssueDrawCommandsStart=24591133217554, SwapBuffers=24591134886554, FrameCompleted=24591136679054, DequeueBufferDuration=219000, QueueBufferDuration=280000, [mali_winsys] EGLint new_window_surface(egl_winsys_display *, void *, EGLSurface, EGLConfig, egl_winsys_surface **, EGLBoolean) returns 0x3000 [libEGL] EGLNativeWindowType 0x7de100a210 disconnect failed [Mono] Requesting loading reference 12 (of 22) of Xamarin.Forms.Platform.Android.dll [Mono] Loading reference 12 of Xamarin.Forms.Platform.Android.dll asmctx DEFAULT, looking for System.Runtime.Serialization, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e [Mono] Assembly Ref addref Xamarin.Forms.Platform.Android[0x7d852aa080] -> System.Runtime.Serialization[0x7d77336a00]: 3 [Mono] Requesting loading reference 21 (of 22) of Xamarin.Forms.Platform.Android.dll [Mono] Loading reference 21 of Xamarin.Forms.Platform.Android.dll asmctx DEFAULT, looking for System.Xml, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e [Mono] Assembly Ref addref Xamarin.Forms.Platform.Android[0x7d852aa080] -> System.Xml[0x7d7769b100]: 6 [libEGL] EGLNativeWindowType 0x7d5d049310 disconnect failed

And Log For Device:

Image Log


Solution

  • I thank everyone and apologize for the inconvenience. The culprit is me for the bad behavior. There was no exception because there really wasn't one. The problem is that the application is launched in the background when you open the files, and in my code I have:

        public static void Background()
        {
            activity.MoveTaskToBack(true);
        } 
    

    I had forgotten. I still need this in another situation, so I resolved this way:

        public static void Background()
        {
            if (App.Current.importClicked) App.Current.importClicked = false;
            else activity.MoveTaskToBack(true);
        } 
    

    Once again I apologize.