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?