Hello,
Welcome to our Microsoft Q&A platform!
From Android Docs https://developer.android.com/training/data-storage/manage-all-files#all-files-access
An app can request All files access from the user by doing the following:
Declare the MANAGE_EXTERNAL_STORAGE permission in the manifest.
Use the ACTION_MANAGE_ALL_FILES_ACCESS_PERMISSION intent action to direct users to a system settings page where they can enable the following option for your app: Allow access to manage all files.
We can judge current version then use Snackbar
to guide the users to open the system page to grand the MANAGE_EXTERNAL_STORAGE
permission.
Firstly, use following code to judge current version and pop a Snackbar
in OnCreate
method of MainActivity
.
public class MainActivity : AppCompatActivity
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
Xamarin.Essentials.Platform.Init(this, savedInstanceState);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.activity_main);
if (Android.OS.Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.R)
{
if (!Environment.IsExternalStorageManager)
{
Snackbar.Make(FindViewById(Android.Resource.Id.Content), "Permission needed!", Snackbar.LengthIndefinite)
.SetAction("Settings", new MyClickHandler(this)).Show();
}
}
}
Then achieve the MyClickHandler
( execute ACTION_MANAGE_ALL_FILES_ACCESS_PERMISSION intent action to direct users to a system settings page) to grand the Manage_External_Storage
permission.
internal class MyClickHandler:Java.Lang.Object ,View.IOnClickListener
{
private MainActivity mainActivity;
public MyClickHandler()
{
}
public MyClickHandler(MainActivity mainActivity)
{
this.mainActivity = mainActivity;
}
public void OnClick(View v)
{
try
{
Uri uri = Uri.Parse("package:" + Application.Context.ApplicationInfo.PackageName);
Intent intent = new Intent(Android.Provider.Settings.ActionManageAppAllFilesAccessPermission, uri);
mainActivity.StartActivity(intent);
}
catch (Exception ex)
{
Intent intent = new Intent();
intent.SetAction(Android.Provider.Settings.ActionManageAppAllFilesAccessPermission);
mainActivity.StartActivity(intent);
}
}
}
And please do not forget to add MANAGE_EXTERNAL_STORAGE
permission to your AndroidManifest.xml
.
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.