Wuhu
Wuhu

Reputation: 181

Xamarin android save text file

I am using Xamarin.Android and I want to save a .txt file to the SD card. Here is the code that I am using:

  private void SavetoSd()
  {
       var sdCardPath = Android.OS.Environment.ExternalStorageDirectory.Path;
       var filePath = System.IO.Path.Combine(sdCardPath, "iootext.txt");
       if (!System.IO.File.Exists(filePath))
       {
           using(System.IO.StreamWriter write = new System.IO.StreamWriter(filePath,true))
           {
               write.Write(etSipServer.ToString());
           }
       }    
  }

However, I receive the following error:

System.UnauthorizedAccessException: Access to the path "/mnt/sdcard/iootext.txt" is denied.

I have added the following to the manifest:

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

How can I fix the error?

Upvotes: 9

Views: 17712

Answers (4)

chrisbyte
chrisbyte

Reputation: 1633

I may be late to the party; but after digging around all day the one thing I found that worked for me is from this link here. The missing piece to my puzzle is to use Environment.SpecialFolder.LocalApplicationData when reading and writing to the file.

A summary example using the details in the link above, slightly modified for ease of use:

string fileName = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "notes.txt");

// read 
string fileData = File.ReadAllText(fileName);

// write
File.WriteAllText(fileName, fileData);

Upvotes: 0

aaron-bond
aaron-bond

Reputation: 3349

If you're on Android 6.0+ you will need to perform a runtime check for permissions. This can be done like so:

if ((CheckSelfPermission(Permission.ReadExternalStorage) == (int)Permission.Granted) && 
    (CheckSelfPermission(Permission.WriteExternalStorage) == (int)Permission.Granted))

More information on this can be found in the android documentation here.

Upvotes: 4

Emil
Emil

Reputation: 6922

I was having same problem and after spending couple of hours, I found that if you are running on sdk higher than 23, android version higher than 6, you should implement access request to user. please find more information here on this link

Upvotes: 0

Shalva Avanashvili
Shalva Avanashvili

Reputation: 899

If file does not exist first create, than get the absolute path and write your data into it.

     Java.IO.File sdCard = Android.OS.Environment.ExternalStorageDirectory;
     Java.IO.File dir = new Java.IO.File (sdCard.AbsolutePath + "/MyFolder");
     dir.Mkdirs ();
     Java.IO.File file = new Java.IO.File (dir,"iootext.txt");
        if (!file.Exists ()) {
            file.CreateNewFile ();
            file.Mkdir ();
            FileWriter writer = new FileWriter (file);
            // Writes the content to the file
            writer.Write (jsonData);
            writer.Flush ();
            writer.Close ();
        }

Upvotes: 3

Related Questions