0

I'm trying to write a file to the internal storage of my phone using Xamarin. So far, I tried to follow this tutorial and found that related question. My code looks as follows:

public async Task WriteDataToFile() {
    string PersonalFolderPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
    System.Diagnostics.Debug.WriteLine("PersonalFolderPath: " + PersonalFolderPath);
    var backingFile = Path.Combine(PersonalFolderPath, "count.txt");
    System.Diagnostics.Debug.WriteLine("backingFile: " + backingFile.ToString());
    if (!System.IO.File.Exists(backingFile)) {
        var newfile = new Java.IO.File(backingFile);
        using (FileOutputStream outfile = new FileOutputStream(newfile)) {
            string line = "The very first line!";
            outfile.Write(System.Text.Encoding.ASCII.GetBytes(line));
            outfile.Close();
        }
    }
}

This is the debug output I receive:

[0:] PersonalFolderPath: /data/user/0/MyApp.Android/files

[0:] backingFile: /data/user/0/MyApp.Android/files/count.txt

After executing the code, I try to access the newly created file from my PC by checking the path "This PC\Galaxy S8\Phone\Android\data\MyApp.Android\files" but unfortunately it's not there. What am I doing wrong here? Help is highly appreciated.

Community
  • 1
  • 1
Hagbard
  • 3,430
  • 5
  • 28
  • 64
  • Can you see or access the file from your s8 phone? – SATYAJEET RANJAN Feb 12 '19 at 18:19
  • 2
    You are looking for the Android method `GetExternalFilesDir` that returns a non-secure path that is external to your app's sandbox, and in the form of application package ID: https://stackoverflow.com/questions/54126671/access-the-android-special-folder-path-by-using-environment/54127487#54127487 – SushiHangover Feb 12 '19 at 18:24
  • Although the debug output path is "/data/user/0/MyApp.Android/files", the actual internal storage path is /data/data/{package name}/files, in your case, the path is /data/data/MyApp.Android/files. – AbbyWang - MSFT Feb 13 '19 at 03:52
  • By the way, to view the file, first you have to add "WRITE_EXTERNAL_STORAGE" permission to your manifest, then you have to root your device. As it is trivial to root the device, I tested using simulator and view the file with adb tool which also need permission, you can use the following command in cmd to grant permission before using adb tool: adb shell pm grant com.companyname.app android.permission.WRITE_EXTERNAL_STORAGE – AbbyWang - MSFT Feb 13 '19 at 06:46
  • @SushiHangover Unfortauntely, the function "GetExternalFilesDir" is not available. "System.Environment.GetExternalFilesDir" doesn't work either. Is there any additional package I have to use here? – Hagbard Feb 13 '19 at 10:22
  • 1
    @Hagbard The methods exists on a context instance (Android application, activity, etc...) https://developer.android.com/reference/android/content/Context#getExternalFilesDir(java.lang.String) – SushiHangover Feb 13 '19 at 13:21
  • 1
    @AbbyWang You do NOT have to root the device, you need no perms, etc.... to access the files in your "app's external dir" (`GetExternalFilesDir`) – SushiHangover Feb 13 '19 at 13:24
  • @SushiHangover Thanks for your advice. I guessed OP wanted to view the internal storage using System.Environment.SpecialFolder.Personal. Maybe I misunderstood it. – AbbyWang - MSFT Feb 14 '19 at 01:42

0 Answers0