xamarin / Essentials

Xamarin.Essentials is no longer supported. Migrate your apps to .NET MAUI, which includes Maui.Essentials.
https://aka.ms/xamarin-upgrade
Other
1.52k stars 505 forks source link

[Bug] FilePicker permission for storage read doesn't work as intended on Android #1537

Closed jarkkokoskinen closed 3 years ago

jarkkokoskinen commented 3 years ago

Description

Recently I started to use Xamarin.Essentials FilePicker for picking files from the file system. When using the FilePicker on Android (Android 10) I noticed that the permission prompt for the storage read permission didn't work properly: When choosing "Deny" from the prompt the file system is still opened, even though it shouldn't. From reading the source code (https://github.com/xamarin/Essentials/blob/main/Xamarin.Essentials/FilePicker/FilePicker.android.cs) it seems that the permission is only asked, but nothing is done with the result (line 19): await Permissions.RequestAsync();.

Steps to Reproduce

  1. Bind the FilePicker PlatformPickAsync method to a button.
  2. Run the application on an Android device (I did this on Android 10).
  3. Press the button that has PlatformPickAsync bound to
  4. From the permission prompt select "Deny"

Expected Behavior

File system doesn't open.

Actual Behavior

File system opens, even though it shouldn't have the right permission. User is still able to navigate the file system and pick files from it.

Basic Information

Workaround

I was able to create a workaround by prompting the permission alert myself before using the FilePicker class' PlatformPickAsync method by using the Xamarin.Essentials Permission class. This didn't prompt a second permission alert from the FilePicker class on Android 10/Samsung SM-G973F, so it fit for my need at the moment.

mattleibow commented 3 years ago

I think this is the incorrect method we are using. It should be EnsureGrantedAsync:

await Permissions.EnsureGrantedAsync<Permissions.StorageRead>();

instead of RequestAsync

mattleibow commented 3 years ago

I think this will also happen in 3 places due to this same error: