Forum Xamarin Xamarin.Forms

Permission of external storage

jon78jon78 Member ✭✭✭

Hi everybody!

I try to run this project
https://github.com/PacktPublishing/Xamarin.Forms-4-Projects/tree/master/Chapter05/GalleryApp/GalleryApp.Android
to study it.

But i get error at first if check

private async Task<bool> Import()
        {
            string[] permissions = { Manifest.Permission.ReadExternalStorage };

            if (MainActivity.Current.CheckSelfPermission(Manifest.Permission.ReadExternalStorage) == Permission.Granted)
            {
                ContinueWithPermission(true);
                return true;
            }

            MainActivity.Current.RequestPermissions(permissions, 33);

            return MainActivity.Current.CheckSelfPermission(Manifest.Permission.ReadExternalStorage) == Permission.Granted;
        }
Tagged:

Best Answer

  • jon78jon78 Member ✭✭✭
    Accepted Answer

    I forgot to set Current on MainActivity

    Current = this;

    sorry

Answers

  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭
    You should use Xamarin Essentials Permission
  • jon78jon78 Member ✭✭✭
    Tks for reply
    How do i do that?
  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭
  • JarvanJarvan Member, Xamarin Team Xamurai
    edited October 19

    To use Xamarin.Essentials.Permission on Android, initialize the Xamarin.Essentials in the OnCreate method first. Then Xamarin.Essentials receive any OnRequestPermissionsResult to handle runtime permissions on Android.

    public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
    {
        public static MainActivity Current { get; private set; }
    
        protected override void OnCreate(Bundle savedInstanceState)
        {
            //...
            base.OnCreate(savedInstanceState);
            Xamarin.Essentials.Platform.Init(this, savedInstanceState); // add this line to your code, it may also be called: bundle
            //...
        }
    
        public override void OnRequestPermissionsResult(int requestCode, string[] permissions, Android.Content.PM.Permission[] grantResults)
        {
            Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);
    
            base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
        }
    }
    

    The required permissions should also be added in the AndroidManifest.xml.

    <manifest xmlns:android="http://schemas.android.com/apk/res/android" ...>
        ...
        <application android:label="xxx"></application>
        <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    </manifest>
    

    Tutorial:
    https://docs.microsoft.com/en-us/xamarin/essentials/permissions?tabs=android

    You can also refer to the code of the MainActivity class and AndroidManifest.xml of the sample link you posted.

  • jon78jon78 Member ✭✭✭
    Accepted Answer

    I forgot to set Current on MainActivity

    Current = this;

    sorry

Sign In or Register to comment.