簡體   English   中英

PDF 文件下載問題 xamarin 中的訪問被拒絕

[英]PDF File Download Issue of Access denied in xamarin Forms Android

我在 Android 清單文件中擁有這些權限

<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”的訪問被拒絕。 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 選項)[0x001aa] 在 /Users/builder/jenkins/workspace/archive-mono/2020-02/android/release/mcs/class/corlib/System.IO/FileStream.cs:239 在 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.Fi leStream..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.IOFile。 .WriteAllBytes(System.String 路徑,System.Byte[] 字節)[0x00039] 在 /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.Z497031794414A552435F90151A C3B54BZ 發件人,System.EventArgs e) [0x000d7] 在 \XYZ.xaml.cs:84 } **

IDE:VS-2019 Android版本:10 Xamarin表格版本:5.0.0.1874

如果針對 API 29+,即使您請求權限並且用戶授予它,您也會收到錯誤,因為他們更改了存儲的工作方式。

但是,如果您像我一樣,厭倦了 Android 讓事情每天變得更加復雜,只需將android:requestLegacyExternalStorage="true"添加到您的清單<application>標簽中,直到您開始針對 ZDB974238714CA8DE634A7CED 為止。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM