I am requesting runtime permissions on button click :
ActivityCompat.RequestPermissions(Activity,
new String[] { Manifest.Permission.ReadExternalStorage, Manifest.Permission.WriteExternalStorage },
100);
The callback for setting the permissions:
public override void OnRequestPermissionsResult(int requestCode, string[] permissions, Android.Content.PM.Permission[] grantResults)
{
base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode)
{
case 100:
{
if (grantResults[0] == Android.Content.PM.Permission.Granted || Build.VERSION.SdkInt >= BuildVersionCodes.Tiramisu)
{
SendNavigationData(NavigatedFrom.StoragePermissionCard);
}
}
break;
default:
Console.WriteLine("Unhandled option : " + requestCode);
break;
}
}
I should be able to access storage on immediate allowing session
Update:
In Maui, you can use Permissions to achieve this.
You can check permissions by code:
and requesting permissions by code:
For more information, please check: Extending permissions.
And there is an official sample, you can check it here: Sample.
I achieved this function on my side, you can refer to the following code:
MainActivity.cs
PermissionUtil.cs
You also need to add the following permissions to
AndroidManifest.xml.For more information, you can check the official document : External storage and Permissions In Xamarin.Android.
And there is an official sample here: RuntimePermissions.