我知道这已经出现了一些时间,但我无法用我尝试的任何解决方案解决错误。
I know this has come up some time but I am not able to solve the error with any of the solutions I tried.
我刚刚开始测试我的应用程序 - 将屏幕截图保存到ios设备代码是 -
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; }要文件
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[]
我得到了例外的主题。我正在使用Unity。
I get the exception that as in subject. I am using Unity.
我得到的错误是 -
Error that I get is -
UnauthorizedAccessException:访问路径/ var / containers / Bundle / Application / 9DA2D489-2037-451E-87D1-FA7354ECD0D1 / Documents被拒绝。
UnauthorizedAccessException: Access to the path "/var/containers/Bundle/Application/9DA2D489-2037-451E-87D1-FA7354ECD0D1/Documents" is denied.
推荐答案
使用保存Application.persistentDataPath 不 Application.dataPath 。请注意,您必须创建一个文件夹并保存在该文件夹中,而不是直接保存到此路径。
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。
或
Application.persistentDataPath +Documents / YourFileNale.extension。
始终使用 Path.Combine 来组合路径而不是+我在上面使用过。
Always use Path.Combine to Combine paths instead of "+" I used above.
更多推荐
UnauthorizedAccessException:访问路径拒绝Ios
发布评论