2

Good day.

In all my applications, requesting user permissions is not showing the permissions popup in Android.

I am using the FileSaver in the .NET MAUI CommunityToolkit and as I understand, the SaveAsync method is supposed to ask for permission and save to disk.

async Task SaveDocument(MemoryStream memoryStream)
{
    try
    {
        //var fileLocation = await FileSaver.Default.SaveAsync($"{Guid.NewGuid()}.docx", memoryStream, new CancellationToken());
        var someLocation = await FileSaver.SaveAsync($"{Guid.NewGuid()}.docx", memoryStream, new CancellationToken());  
        await Toast.Make($"File is saved: {someLocation}").Show();
    }
    catch (Exception ex)
    {
        await Toast.Make($"File is not saved, {ex.Message}").Show();
    }
}

The catch statement is being hit with error message permissions not granted.

Here is my AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
    <application android:allowBackup="true" android:icon="@mipmap/appicon" android:roundIcon="@mipmap/appicon_round" android:supportsRtl="true">
        <provider
            android:name="androidx.core.content.FileProvider"
            android:authorities="${applicationId}.provider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/file_paths" />
        </provider>
    </application>
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.INTERNET" />
</manifest>

As such, I thought I would do it using Permission API in .NET MAUI as implemented below

public static async Task<bool> CheckForStoragePermission()
{
    bool permitted = false;

    var status = await Permissions.CheckStatusAsync<Permissions.StorageWrite>();
    if (status == PermissionStatus.Granted)
        permitted = true;
    else
    {
        status = await Permissions.RequestAsync<Permissions.StorageWrite>();
    }
    return permitted;
}

In both cases, the permissions popup doesn't show up and it moves on with the code. What am I doing wrong?

Tanaka Mawere
  • 683
  • 1
  • 9
  • 22

1 Answers1

4

I have tested your code on the emulator with the Android 12.0 and Android 11.0. The two devices both worked and will show the the permissions popup.

Did you test your application on the Android 13.0? According to this issue about .NET MAUI does not work with Android 13 Android.permission.write_external_storage on the github. The maui project's default target android framework is Android 13.0(Api 33). And then if you set it on the device which is android 13.0. The storage permission popup will not show because the adnrodi 13.0 has removed this permission.

You can try the workaround in the issue which is adding <uses-sdk android:minSdkVersion="21" android:targetSdkVersion="31" /> in the AndroidManifest.xml. You can also use the device which android version is 12.0 or lower to test your application.

Liyun Zhang - MSFT
  • 8,271
  • 1
  • 2
  • 14