Search Unity

  1. Looking for a job or to hire someone for a project? Check out the re-opened job forums.
    Dismiss Notice
  2. The Burst compiler has its own forum section now.
    Dismiss Notice
  3. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

How to write to Android persistentdata file. Unauthorized Access

Discussion in 'Scripting' started by CatchFires, Jan 13, 2021.

  1. CatchFires

    CatchFires

    Joined:
    Jul 19, 2020
    Posts:
    32
    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?
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    12,336
    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.
     
    Bunny83 and CatchFires like this.
  3. CatchFires

    CatchFires

    Joined:
    Jul 19, 2020
    Posts:
    32
    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.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    12,336
    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()
    ...
     
  5. emailadjie

    emailadjie

    Joined:
    Jun 18, 2020
    Posts:
    37
    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")
     
  6. CatchFires

    CatchFires

    Joined:
    Jul 19, 2020
    Posts:
    32
    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"?
     
  7. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    12,336
    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.
     
unityunity