Xamarin android save text file

后端 未结 3 868
Happy的楠姐
Happy的楠姐 2021-01-05 01:24

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()
  {
               


        
相关标签:
3条回答
  • 2021-01-05 01:54

    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 ();
            }
    
    0 讨论(0)
  • 2021-01-05 01:58

    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.

    0 讨论(0)
  • 2021-01-05 02:03

    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

    0 讨论(0)
提交回复
热议问题