How to write to Android persistentdata file. Unauthorized Access

I’ve been scouring google for hours trying to figure out why I can’t write to androids persistentdata path, I’m trying to save a txt file.

I’ve tried to create the directory first and still I get the error, I’ve changed to External access for write permissions and I’ve even tried adding android:requestLegacyExternalStorage=“true” to the manifest, which was suggested all over google.

Could someone please ellaborate on how to Read and Write from Android?

Should just work. You don’t need to create the directory and in fact you can’t. It will already exist.

Some things to try:

First step is always run the game with adb logcat and see if there are any exceptions being thrown.

Then perhaps print out (to console or to a UI object onscreen) the actual path and see if it seems reasonable. Make sure you’re using forward slashes always.

2 Likes

Hey man, thanks for the reply

Thanks for the advice with logcat, I do already use it, would be so lost without it.

Yeah, actually, I legitimately just now got it to work with File.WriteAllText(Application.persistentDataPath + “/Save.txt”, saveData);

Originally what I was trying to do was create another folder so the path would be Application.persistentDataPath + “/SaveData/Save.txt”"

I’ve been staring at it for hours, maybe I’m looking at it wrong but for now I am able to save just in the persistent data folder.

Weird. That should work too. I don’t see anywhere saying that File.WriteAllText() will make the subdirectory though, so that’s probably Directory.CreateDirectory()

I also want to add:
Use Path.Combine() for combining paths, not string concatenation (the plus sign you use)
Example: Path.Combine(Application.persistentDataPath, “/data.bin”)

Yeah, well I tried Directory.CreateDirectory() but i Just couldn’t seem to get it to work, I’ll have another crack at it and see what happens.

I have read that it’s preferable to use Path.Combine but why is that? Isn’t “Path.Combine(Application.persistentDataPath, “/data.bin”)”

the same as

Application.persistentDatapath + “/data.bin”?

I would guess because it reliably handles all combinations of path delimiters on either side?

Plus it is also a params argument if I recall, so you could combine any number of path chunks all at once.