Xamarin : Android : System.UnauthorizedAccessException: Access to the path is denied

前端 未结 5 1789
臣服心动
臣服心动 2020-12-06 16:52

So I\'m trying to create a file and I\'m getting System.UnauthorizedAccessException: Access to the path \"/DownloadJitters\" is denied. I\'m not sure if it\'s a permissions

5条回答
  •  遥遥无期
    2020-12-06 17:24

    Xamarin.Forms (Android solution)

    MainActivity.cs

    • For apps that target Android 5.1(API level 22) or lower, there is nothing more that needs to be done.
    • Apps that will run on Android 6.0(API 23 level 23) or higher should ask Run time permission checks.
    protected override void OnCreate(Bundle bundle)
    {
        if (Build.VERSION.SdkInt >= BuildVersionCodes.M)
        {
            if (!(CheckPermissionGranted(Manifest.Permission.ReadExternalStorage) && !CheckPermissionGranted(Manifest.Permission.WriteExternalStorage)))
            {
                RequestPermission();
            }
        }
        LoadApplication(new App());
    }
    
    private void RequestPermission()
    {
        ActivityCompat.RequestPermissions(this, new string[] { Manifest.Permission.ReadExternalStorage, Manifest.Permission.WriteExternalStorage }, 0);
    }
    
    public bool CheckPermissionGranted(string Permissions)
    {
        // Check if the permission is already available.
        if (ActivityCompat.CheckSelfPermission(this, Permissions) != Permission.Granted)
        {
            return false;
        }
        else
        {
            return true;
        }
    }
    

提交回复
热议问题