我正试图在Android8API级别26模拟器上运行一个Xamarin Android应用程序。当我试图将日志写入文件时,它会给出异常。
异常:
System.UnauthorizedAccessException:拒绝对路径“/存储/模拟/0/abc.txt”的访问
代码:
string filename = "abc.txt";
var documentsPath = Android.OS.Environment.ExternalStorageDirectory.AbsolutePath;
var filePath = Path.Combine(documentsPath, filename);
using (FileStream fs = new FileStream(filePath, FileMode.Append, FileAccess.Write))
using (StreamWriter sw = new StreamWriter(fs))
{
sw.WriteLine(text);
}我还授予了READ_EXTERNAL_STORAGE和WRITE_EXTERNAL_STORAGE权限。
目标安卓版本:Android8.0API级别26 Oreo
异常详细信息:
{ "/storage/emulated/0/NECMobilePos_log21092018.txt“:拒绝对路径System.UnauthorizedAccessException的访问。在/Users/builder/jenkins/workspace/xamarin-android-d15-6/xamarin-android/external/mono/mcs/class/corlib/System.IO/FileStream.cs:239 at System.IO.FileStream..ctor (System.String path,System.IO.FileMode模式,System.IO.FileAccess access,System.IO.FileShare共享,System.Int32 bufferSize,System.Boolean匿名,System.IO.FileOptions选项) 0x001aa (System.String路径,System.IO.FileMode模式,System.IO.FileAccess access)/Users/builder/jenkins/workspace/xamarin-android-d15-6/xamarin-android/external/mono/mcs/class/corlib/System.IO/FileStream.cs:149中的System.IO.FileShare share,System.Int32 bufferSize,System.Boolean isAsync,System.Boolean匿名) 0x00000在System.IO.FileStream..ctor (System.String path,System.IO.FileMode模式,( /Users/builder/jenkins/workspace/xamarin-android-d15-6/xamarin-android/external/mono/mcs/class/corlib/System.IO/FileStream.cs:86 at (包装器远程调用-与检查) System.IO.FileStream..ctor(string,System.IO.FileMode,System.IO.FileAccess) at NECMobilePOS.Client.DroidLatest.FileLogger_Android.SaveText (System.String filenamePassed,System.String text) 0x000d1 in C:\ABC\XYZ\FileLogger_Android.cs:73 }
发布于 2018-09-21 13:34:52
对于版本>= 23,您需要提供运行时权限。您可以从下面的链接中查看文档。
https://devblogs.microsoft.com/xamarin/requesting-runtime-permissions-in-android-marshmallow/
或者现在进行测试,您可以使用app权限并允许存储权限。
https://stackoverflow.com/questions/52442085
复制相似问题