我知道这已经出现了一段时间,但我无法使用我尝试的任何解决方案来解决该错误。
我刚开始测试我的应用程序-将屏幕截图保存到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: Access to the path "/var/containers/Bundle/Application/9DA2D489-2037-451E-87D1-FA7354ECD0D1/Documents" is denied.
最佳答案
您使用 Application.persistentDataPath
not Application.dataPath
保存。请注意,您必须创建一个文件夹并保存在该文件夹中,而不是直接保存到此路径。
所以你保存到的最终路径应该是:
Application.persistentDataPath+"Yourfolder/YourFileNale.extension"
.
或
Application.persistentDataPath+"Documents/YourFileNale.extension"
.
始终使用 Path.Combine
来组合路径,而不是我上面使用的 "+"
。
关于ios - 未授权访问异常 : Access to the path is denies Ios,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45130401/