Search Unity

PersistentDataPath Unauthorized Access Exception

Discussion in 'Android' started by CoopOwnz, Feb 26, 2017.

  1. CoopOwnz

    CoopOwnz

    Joined:
    Oct 6, 2016
    Posts:
    74
    I've seen this question asked a few times but have not found a good answer for me. Those users that have forced the install of my app on external storage will throw an Unauthorized Access Exception when trying to create a file like I show in the code below. This is due to not having permissions to write to external storage. I don't want to ask everyone for permissions to write to external when only a small percentage of users would utilize that permission.

    Code (CSharp):
    1. BinaryFormatter bf = new BinaryFormatter();
    2.         FileStream file;
    3.         if (File.Exists(Application.persistentDataPath + Path.DirectorySeparatorChar + "bj_allin1_data.dat"))
    4.         {
    5.             file = File.Open(Application.persistentDataPath + Path.DirectorySeparatorChar + "bj_allin1_data.dat", FileMode.Open);
    6.         }
    7.         else
    8.         {
    9.             file = File.Create(Application.persistentDataPath + Path.DirectorySeparatorChar + "bj_allin1_data.dat");
    10.         }
    I suppose the solution is to check if the persistentDataPath returned is writable and if it is not then create the file in a location that is.

    https://gist.github.com/gering/f37082d8dd45a3ae4f2c

    I've found that code that has been said to solve the issue but I don't think I understand. How do I find another datapath that is writable? Thanks.

    EDIT: I think the link is what I'm looking for. Are those methods for finding internal and external data paths still valid though? Can anyone elaborate on how this works for me? Thanks
     
    Last edited: Feb 26, 2017
  2. CoopOwnz

    CoopOwnz

    Joined:
    Oct 6, 2016
    Posts:
    74
    I've been reading a lot about this.. I'm seeing that if I have my Write Permission set to internal then the persistentDataPath should always return the internal path. So why am I getting UnauthorizedAccessException to a path that is on an SDCard?

     
  3. GiyomuGames

    GiyomuGames

    Joined:
    Jan 12, 2015
    Posts:
    80
    I have the same question, if someone can help us it'd be great.
    persistentDataPath returning a path on external storage is really problematic for some users :(
     
  4. CoopOwnz

    CoopOwnz

    Joined:
    Oct 6, 2016
    Posts:
    74
    @GiyomuGames In another thread someone posted this example script:

    https://gist.github.com/gering/f37082d8dd45a3ae4f2c

    I implemented that in my own way instead of using persistentDataPath and that resolved almost all of my errors. There are still a few users throwing errors but pretty sure that is their device issue. Anyway, try to use that example script to build your own form of persistentDataPath until Unity resolves it. GL