1

File selection is made with picker in my project where I make Xamarin Forms. The problem is that it always returns null.

my manifest

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

my code

            var customFileType = new FilePickerFileType(new Dictionary<DevicePlatform, IEnumerable<string>>
                {
                    { DevicePlatform.Android, new[] { "application/octet-stream" } }
                });
            var options = new PickOptions
            {
                PickerTitle = "Please select a .DB file",
                FileTypes = customFileType,
            };
            try
            {
                var result = await FilePicker.PickAsync(options); //<= Returns null. But i selected file
                if (result != null)
                {
                    if (result.FileName.EndsWith("db", StringComparison.OrdinalIgnoreCase))
                    {

                        var fileByte = File.ReadAllBytes(result.FullPath);
                        string path = App.path + @"/" + result.FileName;
                        File.WriteAllBytes(path, fileByte);


                        StartViewModel viewModel = new StartViewModel(new StartPage());
                        App.Current.MainPage = viewModel.View;
                    }
                }

            }
            catch (Exception ex)
            {
                // The user canceled or something went wrong
            }

my Output Logs

Resolved pending breakpoint at 'StartPage.xaml.cs:119,1' to void PasswordSaver.Views.StartPage.<PickAndSaveFile>d__5.MoveNext () [0x0008c].
[Timeline] Timeline: Activity_launch_request time:400995680
[Timeline] Timeline: Activity_launch_request time:400995764
[DecorView[]] getWindowModeFromSystem  windowmode is 1
[DecorView] createDecorCaptionView windowingMode:1 mWindowMode 1 isFullscreen: true
[Activity] Slow Operation: Activity com.developerPazl.passwordStore/crc64a0e0a82d0db9a07d.IntermediateActivity onActivityResult took 8205ms
[Looper] PerfMonitor longMsg : seq=1443 plan=09:41:11.653 late=2ms wall=8217ms running=57ms runnable=29ms h=android.app.ActivityThread$H w=159 procState=2
[Choreographer] Frame time is 0.02849 ms in the future!  Check that graphics HAL is generating vsync timestamps using the correct timebase.
[Choreographer] Frame time is 0.009386 ms in the future!  Check that graphics HAL is generating vsync timestamps using the correct timebase.

All packages have been updated. But there is no progress. Your answer will be appreciated.

Edit There is progress

Ok, I'm narrowing down the problem. I opened the simulator there, this code continued to work without any problems. But the problem continues on the Redmi Note 9 Pro device. That doesn't explain how it behaves on specific devices. File Picker not working on certain devices, reason unknown

Emin Niftiyev
  • 197
  • 1
  • 13
  • You might need to first do a ["Run-time permission check"](https://learn.microsoft.com/en-us/xamarin/android/app-fundamentals/permissions?tabs=windows#runtime-permission-checks-in-android-60) to verify that the user has accepted that permission. – ToolmakerSteve Dec 14 '21 at 22:16
  • 2
    @ToolmakerSteve Xamarin.Essentials will throw an exception if permissions are missing – Andrew H Dec 14 '21 at 23:27
  • Are you certain a `.DB` file satisfies the picker options of `application/octet-stream`? – Andrew H Dec 14 '21 at 23:29
  • 1
    Maybe test first with a simple text file. ALSO, where are you calling the code shown from - a button command? Just in case, consider wrapping the call in `Device.BeginInvokeOnMainThread`, to be sure you satisfy this: *"All methods must be called on the UI thread ..."* from https://learn.microsoft.com/en-us/xamarin/essentials/file-picker?tabs=android – ToolmakerSteve Dec 14 '21 at 23:35
  • @ToolmakerSteve this didn't work for me. I appreciate your help. but the method can't return value, I created a separate method but that didn't work either. – Emin Niftiyev Dec 15 '21 at 05:20
  • @AndrewH yes this code was working before. It stopped working for unknown reason. – Emin Niftiyev Dec 15 '21 at 05:22
  • Is there any log about this problem? – Jessie Zhang -MSFT Dec 15 '21 at 05:37
  • @JessieZhang-MSFT i am edit question. added Output info – Emin Niftiyev Dec 15 '21 at 05:43
  • What Android version or API number is on the phone? If different than the emulator, set up an emulated device with same API number. – ToolmakerSteve Dec 15 '21 at 16:42
  • The code was working previously? That’s a very important detail - always mention that when describing a problem. Did it stop after updating VS or XForms? Manually uninstall app from device, to be sure you have a clean start. – ToolmakerSteve Dec 15 '21 at 16:50
  • Also, delete project’s obj and bin folders, to be sure it rebuilds from scratch. – ToolmakerSteve Dec 15 '21 at 16:52
  • @ToolmakerSteve Clearing bin obj is the first thing I do. Yes, this code was working before on the problematic device. After the problem became clear, I decided to Update XForms. The device name is Redmi Note 9 Pro ANDROID-11. API-30 – Emin Niftiyev Dec 16 '21 at 06:29
  • Did you make any changes to your APP before the problem occurred? Has this problem occurred on other devices – Jessie Zhang -MSFT Dec 16 '21 at 07:50
  • @EminNiftiyev if it is a device-specific problem, have you considered trying a different file picker Android app? It could be the current default app for file browsing has an issue with the filter you've provided or something else. – Andrew H Dec 16 '21 at 15:16
  • @JessieZhang-MSFT no i dont any change. – Emin Niftiyev Dec 16 '21 at 15:34
  • @AndrewH yes i tried, same problem occured in all of them. I tried Xamarin.Plugin.FilePicker and Xam.FilePicker – Emin Niftiyev Dec 16 '21 at 15:35
  • Did you **Manually uninstall app from device**? Or might even need to use Tools / Android / adb command line `adb uninstall com.yourcompany.yourappname` to be sure previous debug version didn’t leave something incompatible behind. – ToolmakerSteve Dec 16 '21 at 16:09
  • @EminNiftiyev I do not refer to the NuGet plugin for picking files. I refer to the Android system application for browsing your device file system. The file browser app on your Note 9 may have an incompatibility or other issue. – Andrew H Dec 16 '21 at 19:08
  • @ToolmakerSteve i tried how you said. But nothing working. Thanks for your interest – Emin Niftiyev Dec 17 '21 at 06:10
  • @AndrewH Maybe such problems can occur on MIUI systems. I can't test because I don't have any other MIUI systems. – Emin Niftiyev Dec 17 '21 at 06:11
  • @AndrewH - are you suggesting they write code in Android project that uses a different native file picker? How would they do that? Can you provide more specifics or links? – ToolmakerSteve Dec 17 '21 at 14:43
  • 1
    @ToolmakerSteve Android allows users to pick a "default app" for different actions, like opening files. This is a user action and the developer has no say in what app the user uses to select the file. – Andrew H Dec 17 '21 at 16:58

0 Answers0