0

When I want to write a file to the internal storage/Documents, I don't see any file that was created. Also the path that it returns looks a bit weird.

"/data/user/0/com.companyname.writeexternaldocs/files/temp.txt"

Because I'm working on Android v10/api29 I have to use the mediastore

string fileName = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments), "temp.txt");
File.WriteAllText(fileName, "Some text?");
bool doesExist = File.Exists(fileName);

But when I look at this folder on my android device the temp file is not there. Can anybody point me to the right direction?

Thanks in advance

John
  • 728
  • 2
  • 15
  • 37
  • `...I look at this folder on my android device...` : That folder is in the app's sandbox, how are you "looking" at it? Have you tried opening the file in code (`File.ReadAllText`) – SushiHangover May 05 '20 at 12:18
  • through the file explorer on my desktop and android device – John May 05 '20 at 12:19
  • Unless your phone is rooted, you will not be able to "see it", it is in the app's secure sandbox. – SushiHangover May 05 '20 at 12:20
  • So how do I can "see it"? Because I need to make sure this works... Ps I set the permissions to write and read. – John May 05 '20 at 12:21
  • Read it back via code, or save it to a location that is external to your app and in a publicly accessible location, i.e. https://stackoverflow.com/a/54127487/4984832 – SushiHangover May 05 '20 at 12:22
  • Ok but that still leaves me with the problem of that there is no file in the internal storage/Documents? Is there a way to do this – John May 05 '20 at 12:25
  • Are you trying to save it to the public "Documents" folder of the device? As that is not what your code is doing, you would need to access the `Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryDocuments)` directory – SushiHangover May 05 '20 at 12:27
  • If public means the folder that you can access through the file explorer, yes that one. Like I said, I tried a lot of combinations with the mediastore object because I'am suppose to use that because the old one 'GetExternalStoragePublicDirectory' is deprecated. I'm using Android 10 – John May 05 '20 at 12:30
  • In Android Q/29+, you can use the `ContentResolver` to obtain a "relative path" in regards to public mime-type folders. Android 10 changed the file system completely and thus depending upon what you are trying to do, either has to be if/then based on API level or changed completely to fit in the new file storage system, https://stackoverflow.com/a/56468733/4984832 – SushiHangover May 05 '20 at 12:42
  • I tried what you said (except your latest comment) and indeed I write the file, and I can read it in the app. But in the file explorer of the app it is still not present. I used a different approach now (more or less the same), i use FileSystem.AppDataDirectory now. Any idea why it can write to it, read it but in the file explorer it isn't there? – John May 05 '20 at 13:20
  • If you want to found this path, you should get the root permission. You can open the adb command, input `adb root` to get the root permisson, then open the File explorer in Android device monitor like this sceenshot https://imgur.com/a/iMGrGZ0. Note: `/data/user/0`will be redirected to the `/data/data path` – Leon May 05 '20 at 13:35
  • @John AppDataDirectory is still in the app's secure sandbox – SushiHangover May 05 '20 at 14:19

0 Answers0