我在Android Manifest文件中拥有这些权限
<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" /> 同时在运行时请求权限。
这是我的pdf下载代码
var documentsPath = (string)Android.OS.Environment.GetExternalStoragePublicDirectory("SHAZ");
string localPath = Path.Combine(documentsPath, filename);
File.WriteAllBytes(localPath, bytes);以下是我从android应用程序中获得的内容。
** {System.UnauthorizedAccessException:访问路径"/storage/emulated/0/SHAZ/ABC.pdf“被拒绝。/Users/builder/jenkins/workspace/archive-mono/2020-02/android/release/mcs/class/corlib/System.IO/FileStream.cs:239中的System.IO.FileStream..ctor (System.String路径,System.IO.FileMode模式,System.IO.FileAccess访问,System.IO.FileShare共享,System.Int32 bufferSize,System.Boolean匿名,System.IO.FileOptions选项) 0x001aa在System.IO.FileStream..ctor (System.String路径,System.IO.FileMode模式,System.IO.FileAccess访问,具有检查功能的(包装器远程调用)System.IO.FileStream..ctor( System.IO.FileShare,System.IO.FileMode,System.IO.FileAccess,System.IO.FileShare)在System.IO.File.InternalWriteAllBytes (System.String路径,System.Byte[]字节)位于System.IO.File.WriteAllBytes的/Users/builder/jenkins/workspace/archive-mono/2020-02/android/release/external/corefx/src/System.IO.FileSystem/src/System/IO/File.cs:429中的0x00000 (System.String路径,/Users/builder/jenkins/workspace/archive-mono/2020-02/android/release/external/corefx/src/System.IO.FileSystem/src/System/IO/File.cs:420中位于SHAZ_HR_Payroll.Droid.Models.SaveFile.SaveFiles的0x00039 (System.String文件名,System.Byte[]字节) 0x00034位于SHAZ_HR_Payroll.Pages.Employee.EmployeeChangePasswrod.BtnDownload_Clicked的SaveFile.cs:25中的0x00034 (System.Object发送者,System.EventArgs e) \XYZ.xaml.cs:84中的0x000d7 } **
IDE: VS-2019 Android版本: 10 Xamarin Form版本: 5.0.0.1874
发布于 2021-02-06 23:17:56
如果以29+接口为目标,即使请求权限和用户授权也会得到错误,因为他们改变了存储的工作方式。
但如果你像我一样,厌倦了安卓每天让事情变得更加复杂,只需将android:requestLegacyExternalStorage="true"添加到你的清单<application>标签中,你就可以一直保存到你开始瞄准API30。
https://stackoverflow.com/questions/66077415
复制相似问题