viv*_*una 1 xamarin.android xamarin
我正在尝试在 Android 8 API Level 26 模拟器上运行 Xamarin Android 应用程序。当我尝试将日志写入文件时,它给出了异常。
例外:
System.UnauthorizedAccessException:访问路径“/storage/emulated/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权限。
目标 Android 版本: Android 8.0 API Level 26 Oreo
异常详情:
{System.UnauthorizedAccessException:访问路径“/storage/emulated/0/NECMobilePos_log21092018.txt”被拒绝。在 System.IO.FileStream..ctor(System.String 路径,System.IO.FileMode 模式,System.IO.FileAccess 访问,System.IO.FileShare 共享,System.Int32 bufferSize,System.Boolean 匿名,System.IO. FileOptions 选项)[0x001aa] 在 /Users/builder/jenkins/workspace/xamarin-android-d15-6/xamarin-android/external/mono/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 isAsync,System.Boolean 匿名)[0x00000 ] 在 /Users/builder/jenkins/workspace/xamarin-android-d15-6/xamarin-android/external/mono/mcs/class/corlib/System.IO/FileStream.cs:
小智 7
对于版本 >= 23,您需要提供运行时权限。您可以通过以下链接浏览该文档。
https://devblogs.microsoft.com/xamarin/requesting-runtime-permissions-in-android-marshmallow/
或者现在进行测试,您可以转到应用权限并允许存储权限。
| 归档时间: | 
 | 
| 查看次数: | 4658 次 | 
| 最近记录: |