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.
Exception:
System.UnauthorizedAccessException: Access to the path "/storage/emulated/0/abc.txt" is denied
Code:
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);
}
I have given the READ_EXTERNAL_STORAGE
and WRITE_EXTERNAL_STORAGE
permissions also.
Target Android Version: Android 8.0 API Level 26 Oreo
Exception Details:
{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 }