我正在尝试从字节数组生成 PDF。但它给出了“访问路径被拒绝”的问题。 我已经在 list 文件中授予了权限。我正在发送我的代码块,请检查一下并提供一些建议和反馈,以便我完成它。
private void ObjbtnViewSlip_Click(object sender, EventArgs e)
{
var result = objPaySlipViewModel.GetPaySlipByte(GlobalApplicationSession.EmployeeCode, GlobalApplicationSession.CompanyId, selectedPeriod);
if (File.Exists(Android.OS.Environment.DirectoryDownloads + "/RSI/PaySlip/temp.pdf") == false)
{
Directory.CreateDirectory(Android.OS.Environment.DirectoryDownloads + "/RSI/PaySlip/");
File.Create(Android.OS.Environment.DirectoryDownloads + "/RSI/PaySlip/temp.pdf");
System.IO.File.WriteAllBytes(Android.OS.Environment.DirectoryDownloads + "/RSI/PaySlip/", result);
}
}
最佳答案
首先,您提供的路径不正确。 Android.OS.Environment.DirectoryDownloads 只会返回“下载”
修改您的代码以获取下载文件夹的完整路径,例如:
//The following will return the downloads folder path.
string directory = Path.Combine(Android.OS.Environment.ExternalStorageDirectory.AbsolutePath, Android.OS.Environment.DirectoryDownloads);
string file = Path.Combine(directory, "/RSI/PaySlip/temp.pdf");
同时检查 AndroidManifest.xml 文件中是否提到了以下内容以授予应用程序权限。
uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"
uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
关于android - xamarin android 中的 System.UnauthorizedAccessException : Access to the path "/Download" is denied.,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39263692/