So i am Creating an android app with .net Maui, that reads an XML file from my Documents(/storage/emulated/0/Documents
) folder. The App is 100% capable of generating its own XML file to ensure that it always exists. However if i Replace the XML the Application crashes with an access denied execption. I believe this is due to the Fact that i am Using Storage_Read and Storage_Write Permissions instead of Manage_External_Storage. I am now Looking for a workaround to geting that permission since .net maui has not yet updated its permission system to API level 34.
The code That Requests Permissions:
PermissionStatus lReadStatus = await Permissions.RequestAsync<Permissions.StorageRead>();
PermissionStatus lWriteStatus =await Permissions.RequestAsync<Permissions.StorageWrite>();
My Android Manifest.xml:
<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_DOCUMENTS" />
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" />
Here's an implementation that waits for the activity to exit and then checks if the user gave the permission
public static class StorageHelper
{
public const int RequestCode = 2296;
private static TaskCompletionSource<bool>? GetPermissionTask { get; set; }
public static async Task<bool> GetManageAllFilesPermission()
{
try
{
Uri? uri = Uri.Parse("package:" + Platform.CurrentActivity.ApplicationInfo.PackageName);
GetPermissionTask = new();
Intent intent = new(global::Android.Provider.Settings.ActionManageAppAllFilesAccessPermission, uri);
Platform.CurrentActivity.StartActivityForResult(intent, RequestCode);
}
catch (Exception ex)
{
// Handle Exception
}
return await GetPermissionTask.Task;
}
public static void OnActivityResult()
{
GetPermissionTask?.SetResult(Environment.IsExternalStorageManager);
}
}
MainActivity.cs
protected override void OnActivityResult(int requestCode, Result resultCode, Intent? data)
{
if (requestCode == StorageHelper.RequestCode)
StorageHelper.OnActivityResult();
base.OnActivityResult(requestCode, resultCode, data);
}