3

I'm having a problem with writing to my external sd card. I'm getting a UnauthorizedAccessException whenever I try to write to it. I've already checked WriteExternalStorage in the Android Manifest.
Because GetExternalStoragePublicDirectory, ExternalStorageDirectory etc. are all returning the path to the internal storage, I'm using a manual path, /storage/6363-3065/. If I'm writing to my internal storage, the program runs without error, but it throws an Exception when writing to the sd card.
Can I do anything to solve this? I mean, it can't be impossible, because other programs like my file manager can do it without root or anything...

Edit:

I'm using a LG G4 with Android 6.0. Code is simple:

string filepath = "/storage/6363-3065/Temp/tempfile.temp";
Directory.CreateDirectory(Path.GetDirectoryName(filepath));
AllRight
  • 175
  • 1
  • 11

1 Answers1

0

Try this:

     string text="TestText";
     var dir = System.IO.Path.Combine(Android.OS.Environment.ExternalStorageDirectory.ToString(), "yourAppName");
     if (Directory.Exists(dir) == false)
                {
                    Directory.CreateDirectory(dir);
                }
       string dbFileSDCard = System.IO.Path.Combine(Android.OS.Environment.ExternalStorageDirectory.ToString(), "yourAppName/YourFile.txt");
       System.IO.File.WriteAllText(@dbFileSDCard, text);

do you get errors?

user1230268
  • 221
  • 2
  • 14
  • I don't get any errors, but it doesn't solve my problem either because it just writes to the internal storage. As I wrote, the built-in functions are all returning paths to the internal storage, so I use a manual path to the sd card, but apparently don't have write access there. – AllRight Feb 25 '16 at 20:57
  • have you found this one: http://stackoverflow.com/questions/33139754/android-6-0-marshmallow-cannot-write-to-sd-card – user1230268 Feb 29 '16 at 08:07
  • It's not a problem with the permissions, they are checked and working. The problem probably lays somewhere in Android's way of handling sd cards and I'm searching for a xamarin way of solving this – AllRight Mar 02 '16 at 22:09
  • in android 6 you can decide if you want to use your sd card as internal storage or external storage. look at this: https://motorola-global-portal.custhelp.com/app/answers/prod_answer_detail/a_id/109134/p/1449,9582 Which one did you select? – user1230268 Mar 04 '16 at 15:47
  • I can't between these two, the sd card is automatically portable storage – AllRight Mar 04 '16 at 20:58
  • 1
    have you given your app permission: Settings > Apps > Specific App > to write to sd card? – user1230268 Mar 06 '16 at 17:15