Permissions Android.Xamarin C#

TyrisTyris ITMember ✭✭

I have to enter the permissions management (in particular ReadExternalStorage and WriteExternalStorage) into the MainActivity of an Xamarin Android program, but the documentation available online does not show a step by step procedure. The various solutions found take many things for granted and I can't integrate the code into my program. Can you tell me a practical solution? Thanks.

Tagged:

Best Answer

Answers

  • JarvanJarvan Member, Xamarin Team Xamurai

    I have to enter the permissions management (in particular ReadExternalStorage and WriteExternalStorage) into the MainActivity

    Do you mean declaring the permissions or requesting permissions?
    1.Declare permissions in manifest.xml

    • Double-click Properties in the Solution Explorer and select the Android Manifest tab in the Properties window.
    • Select any permissions your application needs from the Required permissions list and save.

    Tutorial:
    https://docs.microsoft.com/en-us/xamarin/android/app-fundamentals/permissions?tabs=windows#declaring-permissions-in-the-manifest

    2.Request permission.

    if (CheckSelfPermission(Manifest.Permission.WriteExternalStorage) == (int) Permission.Granted)
    {
        //
    }
    

    Refer to:
    https://devblogs.microsoft.com/xamarin/requesting-runtime-permissions-in-android-marshmallow/

  • TyrisTyris ITMember ✭✭

    Thanks for the reply, but the code copied into my activity (C# language) doesn't work.

    This is the best code so far tried, which works in the debug mode with step by step execution, but not in normal mode since the program seems to run ahead in the code without waiting for authorization to access the resources, trying to access a file being read before being authorized!

    protected override void OnCreate(Bundle bundle)
            {           
    
                base.OnCreate(bundle);
    
    
                SetContentView(Resource.Layout.Main);
    
    
                if (ContextCompat.CheckSelfPermission(this, Manifest.Permission.WriteExternalStorage) != (int)Permission.Granted)
                {
                    ActivityCompat.RequestPermissions(this, new string[] { Manifest.Permission.WriteExternalStorage }, 9);
                }
                else
                {
                    return;
                }
    
     public override void OnRequestPermissionsResult(int requestCode, string[] permissions, Permission[] grantResults)
            {
    
    
                    base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
    
            }
    
  • JarvanJarvan Member, Xamarin Team Xamurai

    Try to use Plugin.Permissions plugin to check and request permission. Add the code in activity.

    public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults)
    {
        PermissionsImplementation.Current.OnRequestPermissionsResult(requestCode, permissions, grantResults);
        base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
    }
    

    Check the link: https://github.com/jamesmontemagno/PermissionsPlugin

  • TyrisTyris ITMember ✭✭

    I installed the plugin and integrated the OnRequestPermissionsResult method, but the result is always the same.
    Unfortunately the code flow is not blocked by the instruction

    if (ContextCompat.CheckSelfPermission (this, Manifest.Permission.WriteExternalStorage)! = (int) Permission.Granted)
                 {
                     ActivityCompat.RequestPermissions (this, new string [] {Manifest.Permission.WriteExternalStorage}, 9);
                 }
                 else
                 {
                     return;
                 }
    

    and therefore even before the user gives the authorization for storage, the subsequent statements are executed, which attempt to access the memory, generating the error due to lack of authorization.

    The complete code relating to the permission is the following:

    using Android.App;
    using Android.Widget;
    using Android.OS;
    using System.IO;
    using Android.Views;
    using Android.Support.V4.Content;
    using Android;
    using Android.Content.PM;
    using Android.Support.V4.App;
    using System;
    using Android.Util;
    using Android.Support.Design.Widget;
    using Android.Content;
    using System.Text;
    using Plugin.Permissions;
    using Android.Runtime;
    
    namespace Solver
    {
        [Activity(Label = "Solver", MainLauncher = true, Icon = "@drawable/ic_launcher")]
        public class MainActivity : Android.App.Activity , ActivityCompat.IOnRequestPermissionsResultCallback
        {
    
            protected override void OnCreate(Bundle bundle)
            {           
    
                base.OnCreate(bundle);
    
    
                SetContentView(Resource.Layout.Main);
    
    
                if (ContextCompat.CheckSelfPermission(this, Manifest.Permission.WriteExternalStorage) != (int)Permission.Granted)
                {
                    ActivityCompat.RequestPermissions(this, new string[] { Manifest.Permission.WriteExternalStorage }, 9);
                }
                else
                {
                    return;
                }
    

    .............................

    On Activity:

      public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults)
             {
    
                 PermissionsImplementation.Current.OnRequestPermissionsResult(requestCode, permissions, grantResults);
                 base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
    
             }
    
Sign In or Register to comment.