0

I am creating an app in which I would like to read a file from the sd card and then write to another file using info from the other file. When attempting to read the file I get the following error: System.UnauthorizedAccessException: Access to the path 'sdcard/android/data/App1.App1/files/' is denied.

Read Function:

private string ReadFile(string fileName)
{
      var path = "sdcard/android/data/App1.App1/files/";
      var filePath = Path.Combine(path.ToString(), fileName);
      string text = File.ReadAllText(path.ToString());
      return text;
}

Android Manifest:

    <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="App1.App1" android:versionCode="1" android:versionName="1.0" android:installLocation="auto">
    <uses-sdk android:minSdkVersion="16" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <application android:label="Scout" android:icon="@drawable/Icon" android:theme="@style/CustomActionBarTheme"></application>
</manifest>
jmd613
  • 11
  • 3
  • Please check this tutorial https://developer.xamarin.com/samples/monodroid/StorageClient/ But this is for only storing in the internal memory. – Takermania Jul 05 '17 at 13:55

2 Answers2

0

I have solved the issue and found that I was trying to read a folder instead of the file itself. When switching out the variable path with filePath int the read all text function the error went away.

jmd613
  • 11
  • 3
-1

Do not forget to add

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

into the manifest. This allows to read and write on SD card.

More details about uses-permission

It can also be a problem with your simulator and your sd card, read this topic

Community
  • 1
  • 1
Fabich
  • 2,768
  • 3
  • 30
  • 44
  • I have already done this and I have the ability to write to the sd card but am still unable to read, even from my apps designated directory. As for using a simulator, I am debugging the app from my own phone. – jmd613 Feb 27 '16 at 15:10
  • Permissions are not really concern in XAMARIN. By default the platform will takecare. – Takermania Jul 05 '17 at 13:52
  • Here the issue, Using XAMARIN Cross Platform development tool.. It is very hard to read & write the file/folders from the SD Card. – Takermania Jul 05 '17 at 13:54