I have been updating my app to comply with Googles requirements of API level, and permission use.
It seems that no matter what I try, I cannot get any permissions in my app. I have tried two different methods (code below). I am never prompted to approve the permissions. Furthermore, I've also tried manually setting the permissions in the settings->app->app permissions. The app reports the permission has been denied.
My android manifest includes the following permissions.
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.MANAGE_MEDIA" />
<uses-permission android:name="android.permission.MANAGE_DOCUMENTS" />
My android MainActivity
has the code
protected override async void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
Xamarin.Essentials.Platform.Init(this, savedInstanceState);
global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
....
The code I am using to check on, and request, the permissions is.
private async void BtnRestoreDatbase_Clicked(object sender, EventArgs e)
{
try
{
// Check that we have access to be playing with these files.
var status = await CrossPermissions.Current.CheckPermissionStatusAsync<StoragePermission>();
if (status != PermissionStatus.Granted)
{
if (await CrossPermissions.Current.ShouldShowRequestPermissionRationaleAsync(Permission.Storage))
{
await Application.Current.MainPage.DisplayAlert("File Access", StringTools.GetStringResource("szAccessFiles"), StringTools.GetStringResource("szOK"));
// Request the permissions from the system again.
status = await CrossPermissions.Current.RequestPermissionAsync<StoragePermission>();
}
}
// We got permission to access the files, so let's do it.... (what a worthless comment.....)
if (status == Plugin.Permissions.Abstractions.PermissionStatus.Granted)
{
........
Since this approach doesn't work, I tried making my own calls that would use the Xamarin.Essentials
library too.
public static async Task<PermissionStatus> CheckAndRequestStoragePermissionRead()
{
var status = await Permissions.CheckStatusAsync<Permissions.StorageRead>();
if (status == PermissionStatus.Granted)
return status;
if (status == PermissionStatus.Denied && DeviceInfo.Platform == DevicePlatform.iOS)
{
// Prompt the user to turn on in settings
// On iOS once a permission has been denied it may not be requested again from the application
return status;
}
if (Permissions.ShouldShowRationale<Permissions.StorageRead>())
{
// Prompt the user with additional information as to why the permission is needed
}
status = await Permissions.RequestAsync<Permissions.StorageRead>();
return status;
}
Neither approach is working. The code worked in previous versions of Android, so I am not sure what has changed?