App permissions denied, even when manually set

Jesse Knott 686 Reputation points
2023-09-26T17:59:39.9433333+00:00

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?

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,334 questions
0 comments No comments
{count} votes

Accepted answer
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 74,081 Reputation points Microsoft Vendor
    2023-09-27T02:29:58.3166667+00:00

    Hello,

    If your app targets Android 11, both the WRITE_EXTERNAL_STORAGE permission and the WRITE_MEDIA_STORAGE privileged permission no longer provide any additional access.

    From this Goole document about [Android 11 storage].(https://developer.android.com/about/versions/11/privacy/storage#permissions-target-11)

    You can write/read your file in the internal application storage with Xamarin.Essentials: File System Helpers, and you do not need to request READ_EXTERNAL_STORAGE and WRITE_EXTERNAL_STORAGE permission.

    Best Regards,

    Leon Lu


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.