How to Request manage_external_storage Permission in Xamarin.Android

SimonGhost 256 Reputation points
2021-11-10T11:16:40.56+00:00

Hello Everyone :)
Please I am looking for a way to request (Manage_External_Storage) Permission in Xamarin.Android because i need this permission in my app after android 11
I did not find it in Xamarin.Essentials.
thanks in advance :)

Developer technologies | .NET | Xamarin
0 comments No comments
{count} votes

Accepted answer
  1. Anonymous
    2021-11-11T03:08:21.8+00:00

    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.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.