Arvind Chourasiya
Arvind Chourasiya

Reputation: 17382

System.UnauthorizedAccessException: Access to the path denied- Xamarin

I have PDF file to read but getting exception

System.UnauthorizedAccessException: Access to the path "/storage/emulated/0/geometry.pdf" is denied.

Downloadbtn_click method below

if (ContextCompat.CheckSelfPermission(this.Context, Manifest.Permission.ReadExternalStorage) != Permission.Granted)
{
   ActivityCompat.RequestPermissions(this.Activity, new String[] { Manifest.Permission.ReadExternalStorage }, 1);
}
else
{
    var externalPath = global::Android.OS.Environment.ExternalStorageDirectory.Path + "/" + fileName;
    //Below line getting exception
    System.IO.File.WriteAllBytes(externalPath, PdfBytes);
    var pdfPath = Android.Net.Uri.FromFile(new Java.IO.File(externalPath));
    Intent intent = new Intent(this.Activity.Intent.Action);
    intent.SetDataAndType(pdfPath, "application/pdf");
    this.Context.StartActivity(intent);
}

Overriding OnRequestPermissionsResult method

public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Permission[] grantResults)
{
base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode)
{
    case 1:
    {
        if (grantResults.Length > 0&& grantResults[0] == Permission.Granted)
        {
            var externalPath = global::Android.OS.Environment.ExternalStorageDirectory.Path + "/" + fileName;
            System.IO.File.WriteAllBytes(externalPath, bt);
            var pdfPath = Android.Net.Uri.FromFile(new Java.IO.File(externalPath));
            Intent intent = new Intent(this.Activity.Intent.Action);
            this.Context.StartActivity(intent);
        }
        return;
    }
}
}

Menifest file

<uses-sdk android:minSdkVersion="17" android:targetSdkVersion="27" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

Method OnRequestPermissionsResult never getting called. When clicking on Download button control directly going to else part as permission already provided in Menifest.xml file.

Exception getting at

System.IO.File.WriteAllBytes(externalPath, PdfBytes);

How can I solve this irritating issue.

Upvotes: 1

Views: 5815

Answers (1)

Access Denied
Access Denied

Reputation: 9461

Welcome to Android 8. I also had this problem.

Turns out ReadExternalStorage is not sufficient anymore. If you go to app settings you will see that there is no read or write setting, only File access. To fix the problem request both read and write permissions and check them both as well:

{ Manifest.Permission.ReadExternalStorage, Manifest.Permission.WriteExternalStorage }

Is not it about writing? System.IO.File.WriteAllBytes(externalPath, PdfBytes);

According to the docs it should work for reading, but in fact it didn't work for me.

According to docs:

Permissions

Prior to Android 8.0 (API level 26), if an app requested a permission at runtime and the permission was granted, the system also incorrectly granted the app the rest of the permissions that belonged to the same permission group, and that were registered in the manifest.

For apps targeting Android 8.0, this behavior has been corrected. The app is granted only the permissions it has explicitly requested. However, once the user grants a permission to the app, all subsequent requests for permissions in that permission group are automatically granted.

For example, suppose an app lists both READ_EXTERNAL_STORAGE and WRITE_EXTERNAL_STORAGE in its manifest. The app requests READ_EXTERNAL_STORAGE and the user grants it. If the app targets API level 25 or lower, the system also grants WRITE_EXTERNAL_STORAGE at the same time, because it belongs to the same STORAGE permission group and is also registered in the manifest. If the app targets Android 8.0 (API level 26), the system grants only READ_EXTERNAL_STORAGE at that time; however, if the app later requests WRITE_EXTERNAL_STORAGE, the system immediately grants that privilege without prompting the user.

Upvotes: 1

Related Questions