我想從我的streamingAssets中恢復文件並在persistentDataPath中創建它的副本。Unity - IOS persistentDataPath訪問被拒絕//無法打開數據庫
的問題是,獲得persistentDataPath由iOS的拒絕,所以我不能寫文件,有人可以告訴我爲什麼?
代碼:
#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 團結:5.5.0f3
----------------------------------------- -----------------------
UPDATE: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
第一如果s代理工作了一會兒(沒有combine
),但現在,花生。
第二個返回true(Exists
),但是當它到達using語句,它說:「找不到路徑的一部分」(即使它發現它的if語句,跆拳道?)
------------------------------------------------ ----------------
更新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();
}
因此正確pathseems是:
string bpa = Application.dataPath + "/Raw/" + StaticDatas.databaseName;
---------------------------- ------------------------------------
UPDATE 4:
好的,我想我知道問題是什麼(即使我不明白它):
我現在可以從流的資產得到我的DATAS並將其複製到persistentDatapath,但是當我創建一個文件的連接時,SQLite拋出一個異常:
Could not open database file
是否有人知道爲什麼嗎?
--------------------------------------------- ------------------- 更新5:
我做一個組合創建路徑「targetPath」,在日誌中,它顯示「/ var /移動/容器/數據/應用/ manylettersanddigits /文檔/ database.db」
然而,在把SQLException,它示出了相同但沒有和文檔之間的斜線database.db
[this]的副本(http://stackoverflow.com/questions/41433761/file-create-fails-on-iphone-5s)? – Programmer
將看看它,謝謝 – Andromelus
我很困惑...你爲什麼要問'Application.persistantDataPath',但在你的代碼中,你正在使用'Application.dataPath?'我很確定http:// answers.unity3d.com/questions/209108/when-to-use-persistentdatapath-versus-datapath.html回答你的問題。 – Zze