错误访问路径“/storage/emulated/0/abc.txt";在 Xamarin Android 中被拒绝 [英] Error Access to the path "/storage/emulated/0/abc.txt" is denied in Xamarin Android

查看:241
本文介绍了错误访问路径“/storage/emulated/0/abc.txt";在 Xamarin Android 中被拒绝的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 Android 8 API 级别 26 模拟器上运行 Xamarin Android 应用程序.当我尝试将日志写入文件时,它给出了异常.

I am trying to run a Xamarin Android app on Android 8 API Level 26 emulator. Its giving exception when I am trying to write logs to file.

例外:

System.UnauthorizedAccessException:访问路径/storage/emulated/0/abc.txt"被拒绝

System.UnauthorizedAccessException: Access to the path "/storage/emulated/0/abc.txt" is denied

代码:

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_STORAGEWRITE_EXTERNAL_STORAGE 权限.

I have given the READ_EXTERNAL_STORAGE and WRITE_EXTERNAL_STORAGE permissions also.

目标 Android 版本:Android 8.0 API Level 26 Oreo

Target Android Version: 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 path, 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:149在 System.IO.FileStream..ctor (System.String path, System.IO.FileMode模式,System.IO.FileAccess 访问)[0x00000] 中/Users/builder/jenkins/workspace/xamarin-android-d15-6/xamarin-android/external/mono/mcs/class/corlib/System.IO/FileStream.cs:86at(包装器远程调用检查)System.IO.FileStream..ctor(string,System.IO.FileMode,System.IO.FileAccess)在 NECMobilePOS.Client.DroidLatest.FileLogger_Android.SaveText(System.String filenamePassed, System.String text) [0x000d1] inC:\ABC\XYZ\FileLogger_Android.cs:73 }

{System.UnauthorizedAccessException: Access to the path "/storage/emulated/0/NECMobilePos_log21092018.txt" is denied. 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 options) [0x001aa] in /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 mode, System.IO.FileAccess access, System.IO.FileShare share, System.Int32 bufferSize, System.Boolean isAsync, System.Boolean anonymous) [0x00000] in /Users/builder/jenkins/workspace/xamarin-android-d15-6/xamarin-android/external/mono/mcs/class/corlib/System.IO/FileStream.cs:149 at System.IO.FileStream..ctor (System.String path, System.IO.FileMode mode, System.IO.FileAccess access) [0x00000] in /Users/builder/jenkins/workspace/xamarin-android-d15-6/xamarin-android/external/mono/mcs/class/corlib/System.IO/FileStream.cs:86 at (wrapper remoting-invoke-with-check) 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 }

推荐答案

对于版本 >= 23,您需要提供运行时权限.您可以通过以下链接浏览该文档.

For Versions >= 23, you need to provide the Runtime Permissions. You can go through the document from the link below.

https://devblogs.microsoft.com/xamarin/requesting-runtime-permissions-in-android-marshmallow/

或者现在进行测试,您可以转到应用权限并允许存储权限.

Or for testing now, you can go to app permissions and allow storage permission.

这篇关于错误访问路径“/storage/emulated/0/abc.txt";在 Xamarin Android 中被拒绝的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆