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.53k stars 505 forks source link

[Bug][Android] MediaPicker photo/video capture requires WRITE_EXTERNAL_STORAGE #1761

Open rickclephas opened 3 years ago

rickclephas commented 3 years ago

Description

The current implementation of PlatformCaptureAsync requires the WRITE_EXTERNAL_STORAGE permission. This permission is only needed to write files outside application folders. Captured photos and videos are stored in the application cache folder which doesn't require any permission on API level 19 and above.

I don't see any other reason why the WRITE_EXTERNAL_STORAGE permission should be required. In case this permission is required then the functionality would break on Android 11 since it doesn't provide additional access anymore: https://developer.android.com/about/versions/11/privacy/storage#permissions-target-11.

Expected Behavior

The WRITE_EXTERNAL_STORAGE permission isn't required to capture a photo or video.

Actual Behavior

Incase the WRITE_EXTERNAL_STORAGE permission isn't added to the app the following check fails: https://github.com/xamarin/Essentials/blob/7218ab88f7fbe00ec3379bd54e6c0ce35ffc0c22/Xamarin.Essentials/MediaPicker/MediaPicker.android.cs#L63

bohdanfk21 commented 3 years ago

it would be good, if we could add android:maxSdkVersion in our permission

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

I think this part of code will work

 if(Android.OS.Build.VERSION.SdkInt <= Android.OS.BuildVersionCodes.P)
      await EnsureGrantedAsync<Permissions.StorageWrite>();
      ...
 if (Android.OS.Build.VERSION.SdkInt <= Android.OS.BuildVersionCodes.P)
    capturePhotoIntent.AddFlags(ActivityFlags.GrantWriteUriPermission);