0
votes

I have these permissions in the Android Manifest file

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 

also requesting permission at runtime.

Here is my pdf download code

var documentsPath = (string)Android.OS.Environment.GetExternalStoragePublicDirectory("SHAZ");
string localPath = Path.Combine(documentsPath, filename);
File.WriteAllBytes(localPath, bytes);

Here is what I am getting from the android app.

** {System.UnauthorizedAccessException: Access to the path "/storage/emulated/0/SHAZ/ABC.pdf" is denied. at System.IO.FileStream..ctor (System.String path, System.IO.FileMode mode, System.IO.FileAccess access, System.IO.FileShare share, System.Int32 bufferSize, System.Boolean anonymous, System.IO.FileOptions options) [0x001aa] in /Users/builder/jenkins/workspace/archive-mono/2020-02/android/release/mcs/class/corlib/System.IO/FileStream.cs:239 at System.IO.FileStream..ctor (System.String path, System.IO.FileMode mode, System.IO.FileAccess access, System.IO.FileShare share) [0x00000] in /Users/builder/jenkins/workspace/archive-mono/2020-02/android/release/mcs/class/corlib/System.IO/FileStream.cs:91 at (wrapper remoting-invoke-with-check) System.IO.FileStream..ctor(string,System.IO.FileMode,System.IO.FileAccess,System.IO.FileShare) at System.IO.File.InternalWriteAllBytes (System.String path, System.Byte[] bytes) [0x00000] in /Users/builder/jenkins/workspace/archive-mono/2020-02/android/release/external/corefx/src/System.IO.FileSystem/src/System/IO/File.cs:429 at System.IO.File.WriteAllBytes (System.String path, System.Byte[] bytes) [0x00039] in /Users/builder/jenkins/workspace/archive-mono/2020-02/android/release/external/corefx/src/System.IO.FileSystem/src/System/IO/File.cs:420 at SHAZ_HR_Payroll.Droid.Models.SaveFile.SaveFiles (System.String filename, System.Byte[] bytes) [0x00034] in SaveFile.cs:25 at SHAZ_HR_Payroll.Pages.Employee.EmployeeChangePasswrod.BtnDownload_Clicked (System.Object sender, System.EventArgs e) [0x000d7] in \XYZ.xaml.cs:84 } **

IDE: VS-2019 Android Version: 10 Xamarin Form Version: 5.0.0.1874

1
That has not much to do with downloading. I think nothing. It has all to do with trying to create a file in storage.blackapps
Yes, you are right. While saving file system is giving error of access denied, although I gave full permission.Zohaib Hassan
No, you requested permission. But you use an Android 10 device.blackapps

1 Answers

0
votes

If targeting API 29+, you will get the error even if you request the permission and user grants it, because they changed how storage works.

But if you are like me, tired of Android making things more complicated every day, just add android:requestLegacyExternalStorage="true" to your manifest <application> tag and you are saved until you start targeting API 30.