3

I know this has come up some time but I am not able to solve the error with any of the solutions I tried.

I just started testing my application - to save a screenshot to ios device Code is -

string GetiPhoneDocumentsPath()
{
    string path = Application.dataPath.Substring(0, Application.dataPath.Length - 5);
    path = path.Substring(0, path.LastIndexOf("/"));
    return path + "/Documents/";
}

string CreateImagesDirectory(string documentsPath) {
    //System.IO.File.SetAttributes (documentsPath, FileAttributes.Normal);
    string imagePath = documentsPath +"magicimages/";
    if (Directory.Exists(imagePath)) {return imagePath;}
    DirectoryInfo t = new DirectoryInfo(documentsPath);
    //Directory.CreateDirectory(imagePath);
    t.CreateSubdirectory("magicimages");
    System.IO.File.SetAttributes (imagePath, FileAttributes.Normal);

    return imagePath;
}

To wite the file

    Debug.Log("Do nothing actually as we need to save to persistent data path");
        string documentsPathIphone = GetiPhoneDocumentsPath();
        Debug.Log ("document path" + documentsPathIphone);
        string imagePath = CreateImagesDirectory (documentsPathIphone);
        //Path = imagePath + fileName;
        Debug.Log ("path iphone" + Path);
        Debug.Log("Appllicarion data path -->" + Application.dataPath);
        //string savepath = Application.dataPath.Replace ("game.app/Data", "/Documents/");
        System.IO.File.WriteAllBytes (System.IO.Path.Combine(Path , fileName), screenshot);

Assuming screenshot is bytes[]

I get the exception that as in subject. I am using Unity.

Error that I get is -

UnauthorizedAccessException: Access to the path "/var/containers/Bundle/Application/9DA2D489-2037-451E-87D1-FA7354ECD0D1/Documents" is denied.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Kaushik Ray
  • 555
  • 2
  • 8
  • 21

1 Answers1

8

You save with Application.persistentDataPath not Application.dataPath. Note that you must create a folder and save inside that folder instead of saving directly to this path.

So the final path you save to should be:

Application.persistentDataPath+"Yourfolder/YourFileNale.extension".

or

Application.persistentDataPath+"Documents/YourFileNale.extension".

Always use Path.Combine to Combine paths instead of "+" I used above.

Programmer
  • 121,791
  • 22
  • 236
  • 328
  • Before I can create the directory I get the error in CreateImagesDirectory() so even the folder creation is not happening – Kaushik Ray Jul 16 '17 at 16:07
  • What do you expect me to do if you get an error and don't post it? ............ `if (!Directory.Exists(Path.GetDirectoryName(yourPath))) { Directory.CreateDirectory(Path.GetDirectoryName(yourPath)); }`...... then ....... `File.WriteAllBytes(yourPath, yourData);` – Programmer Jul 16 '17 at 16:21
  • ok, I updated the methods with the code and error that I am getting. the exists option is available in CreateImagesDirectory method. Can you please check now? – Kaushik Ray Jul 16 '17 at 16:41
  • You also need to add **Edit** and add the new code ans also mention the line of coed that's causing that error. – Programmer Jul 16 '17 at 19:10
  • I solved it actually now. The issue was with datapath vs persistentdata path. using persistent one resolved the issue. Thanks anyways – Kaushik Ray Jul 17 '17 at 14:55
  • 2
    This is what I said in my answer. I said you should use `Application.persistentDataPath` **not** `Application.dataPath`. I don't know what you have been using since then. – Programmer Jul 17 '17 at 18:18