[英]How access to persistentDataPath child folders on IOS (Unity Deployment)?
[英]Unity - IOS persistentDataPath access denied // Could not open database
我試圖從我的streamingAssets恢復文件,並在persistentDataPath中制作一個副本。
問題是iOS拒絕了對persistentDataPath的訪問,因此我無法編寫該文件,有人可以告訴我為什么嗎?
碼:
#elif UNITY_IOS
if (File.Exists(Application.dataPath + "/Raw/" + StaticDatas.databaseName))
{
byte[] bytes = null;
if (File.Exists(Application.dataPath + "/Raw/" + StaticDatas.databaseName))
{
Debug.Log(Application.dataPath + "/Raw/" + StaticDatas.databaseName);
Debug.Log(StaticDatas.databasePath + StaticDatas.databaseName);
using (FileStream fileStream = File.OpenRead(Application.dataPath + "/Raw/" + StaticDatas.databaseName))
{
bytes = new byte[fileStream.Length];
fileStream.Read(bytes,0,int.Parse(fileStream.Length+""));
fileStream.Close();
fileStream.Dispose();
}
FileStream fs = File.Create(StaticDatas.databasePath + StaticDatas.databaseName);
fs.Write(bytes, 0, bytes.Length);
}
_connection = new SQLiteConnection(filePath, SQLiteOpenFlags.ReadWrite | SQLiteOpenFlags.Create);
CreateDB();
}
錯誤:拒絕訪問路徑/var/mobile/containers/dta/application/manythings/DocumentsDatabaseName.db“
iOS:9.5.3 Unity:5.5.0f3
-------------------------------------------------- --------------
更新:StaticDatas.databasePath = persistentDataPath
-------------------------------------------------- --------------
更新2:
好吧,我完全迷路了...
這是我的代碼:
#elif UNITY_IOS
string basePath = Path.Combine(Application.dataPath + "Raw" , StaticDatas.databaseName);
string targetPath = Path.Combine(StaticDatas.databasePath , StaticDatas.databaseName );
Debug.Log(basePath);
Debug.Log(targetPath);
if (File.Exists(basePath))
{
byte[] bytes = null;
Debug.Log("base path exists");
using (FileStream fileStream = File.OpenRead(basePath))
{
Debug.Log("create byte array");
bytes = new byte[fileStream.Length];
Debug.Log(" READ BYTES");
fileStream.Read(bytes,0,int.Parse(fileStream.Length+""));
Debug.Log(" CLOSE");
fileStream.Close();
Debug.Log("DISPOSE");
fileStream.Dispose();
}
Debug.Log(" Check if dir exists");
/* if (!Directory.Exists(StaticDatas.databasePath + "/AnnotationTest.app/database/"))
{
Debug.Log(" create folder");
Directory.CreateDirectory(StaticDatas.databasePath + "/AnnotationTest.app/database/");
}*/
Debug.Log("Open file");
FileStream fs = File.Open(targetPath, FileMode.OpenOrCreate);
Debug.Log("Write file");
fs.Write(bytes, 0, bytes.Length);
Debug.Log(" CLOSE STRREAM");
fs.Close();
Debug.Log("Connec close");
_connection = new SQLiteConnection(targetPath, SQLiteOpenFlags.ReadWrite | SQLiteOpenFlags.Create);
Debug.Log("sql connect");
CreateDB();
Debug.Log(" db made");
}
if (File.Exists("file:" + Application.dataPath + "/Raw/" + StaticDatas.databaseName));
{
Debug.Log("file datapath raw databasename");
byte[] bytes = null;
Debug.Log("base path exists");
using (FileStream fileStream = File.OpenRead("file:" + Application.dataPath + "/Raw/" + StaticDatas.databaseName))
{
Debug.Log("create byte array");
bytes = new byte[fileStream.Length];
Debug.Log(" READ BYTES");
fileStream.Read(bytes,0,int.Parse(fileStream.Length+""));
Debug.Log(" CLOSE");
fileStream.Close();
Debug.Log("DISPOSE");
fileStream.Dispose();
}
FileStream fs = File.Open(targetPath, FileMode.OpenOrCreate);
Debug.Log("Write file");
fs.Write(bytes, 0, bytes.Length);
Debug.Log(" CLOSE STRREAM");
fs.Close();
_connection = new SQLiteConnection(targetPath, SQLiteOpenFlags.ReadWrite | SQLiteOpenFlags.Create);
CreateDB();
}
#else
var loadDb = StaticDatas.databasePath + StaticDatas.databaseName;
File.Copy(loadDb, filePath);
_connection = new SQLiteConnection(filePath, SQLiteOpenFlags.ReadWrite | SQLiteOpenFlags.Create);
CreateDB();
#endif
第一個if語句確實起作用了一段時間(不使用combine
),但是現在起作用了。
第二個返回true( Exists
),但是當到達using語句時,它說“找不到路徑的一部分”(即使在if語句中找到了wtf?)
-------------------------------------------------- --------------
更新3
這是一個奇跡! 我找到了! 好了,現在,SQLite無法打開數據庫,但是我找到了!
string bpa = Application.dataPath + "/Raw/" + StaticDatas.databaseName;
if (File.Exists(bpa))
{
byte[] b = null;
using (FileStream fs = File.OpenRead(bpa))
{
b = new byte[fs.Length];
fs.Read(b, 0, int.Parse(fs.Length+""));
fs.Close();
fs.Dispose();
}
FileStream fsa = File.Open(targetPath, FileMode.OpenOrCreate);
fsa.Write(b,0, b.Length);
fsa.Close();
_connection = new SQLiteConnection(targetPath, SQLiteOpenFlags.ReadWrite | SQLiteOpenFlags.Create);
CreateDB();
}
else if (File.Exists(basePath))
{
byte[] b = null;
using (FileStream fs = File.OpenRead(basePath))
{
b = new byte[fs.Length];
fs.Read(b, 0, int.Parse(fs.Length+""));
fs.Close();
fs.Dispose();
}
FileStream fsa = File.Open(targetPath, FileMode.OpenOrCreate);
fsa.Write(b,0, b.Length);
fsa.Close();
_connection = new SQLiteConnection(targetPath, SQLiteOpenFlags.ReadWrite | SQLiteOpenFlags.Create);
CreateDB();
}
因此正確的路徑似乎是:
string bpa = Application.dataPath + "/Raw/" + StaticDatas.databaseName;
-------------------------------------------------- --------------
更新4:
好的,我想我知道問題出在哪里(即使我不理解):
現在,我可以從流媒體資產中獲取數據並將其復制到persistentDatapath,但是當我與該文件創建連接時,SQLite會引發異常:
Could not open database file
有人知道為什么嗎?
-------------------------------------------------- --------------更新5:
我做一個組合來創建路徑“ targetPath”,在日志中,它顯示為“ /var/mobile/Container/Data/Application/manylettersanddigits/Documents/database.db”
但是,在SQLException中,它顯示相同的內容,但在Documents和database.db之間沒有斜線
在IOS上,可以通過以下方式訪問流媒體資產的相關路徑:
Application.dataPath + "/Raw/" + StaticDatas.databaseName;
在這種情況下,造成“無法打開數據庫文件”的原因是因為我在該方法中創建了2個連接(令我感到羞恥)。的確,第二個正在使之成為buggig(兩者均相同),這是我的錯誤。
謝謝大家。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.