【问题标题】:UnauthorizedAccessException: Access to the path is denies IosUnauthorizedAccessException: 对路径的访问被拒绝 Ios
【发布时间】:2017-07-16 15:40:51
【问题描述】:

我知道这已经出现了一段时间,但我无法使用我尝试的任何解决方案来解决该错误。

我刚开始测试我的应用程序-将屏幕截图保存到ios设备代码是-

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;
}

写入文件

    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);

假设截图是字节[]

我得到了主题中的异常。我正在使用 Unity。

我得到的错误是 -

UnauthorizedAccessException: 访问路径 “/var/containers/Bundle/Application/9DA2D489-2037-451E-87D1-FA7354ECD0D1/Documents” 被拒绝。

【问题讨论】:

    标签: ios unity3d vuforia access-denied


    【解决方案1】:

    你保存Application.persistentDataPath 不是 Application.dataPath。请注意,您必须创建一个文件夹并保存在该文件夹中,而不是直接保存到此路径。

    所以你保存到的最终路径应该是:

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

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

    始终使用Path.Combine 来组合路径,而不是我上面使用的"+"

    【讨论】:

    • 在创建目录之前,我在 CreateImagesDirectory() 中收到错误,所以即使文件夹创建也没有发生
    • 如果您遇到错误但不发布,您希望我怎么做? …………if (!Directory.Exists(Path.GetDirectoryName(yourPath))) { Directory.CreateDirectory(Path.GetDirectoryName(yourPath)); }……然后…………File.WriteAllBytes(yourPath, yourData);
    • 好的,我用我得到的代码和错误更新了方法。存在选项在 CreateImagesDirectory 方法中可用。你现在可以检查一下吗?
    • 您还需要添加 Edit 并添加新代码,还需要提及导致该错误的 coed 行。
    • 这就是我在回答中所说的。我说你应该使用Application.persistentDataPath Application.dataPath。我不知道你从那以后一直在用什么。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-21
    • 1970-01-01
    • 2019-05-26
    • 2021-04-02
    • 1970-01-01
    • 2017-01-24
    • 1970-01-01
    相关资源
    最近更新 更多