Reputation: 2972
I am trying to recover a file from my streamingAssets and make a copy of it in the persistentDataPath.
The problem is that access to persistentDataPath is denied by iOS, so I cannot write the file, can someone tell me why?
Code:
#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();
}
Error: Access to the path /var/mobile/containers/dta/application/manythings/DocumentsDatabaseName.db " is denied
IOS: 9.5.3 Unity: 5.5.0f3
----------------------------------------------------------------
UPDATE: StaticDatas.databasePath = persistentDataPath
----------------------------------------------------------------
UPDATE 2:
Well, I am completely lost...
Here is the code I have:
#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
The first if statement did work for a moment (without combine
), but now, peanuts.
The second one returns true (Exists
), but when it reaches the using statement, it says "Could not find a part of the path" (Even if it finds it in the if statement, wtf?)
----------------------------------------------------------------
UPDATE 3
It's a miracle! I found it! Wel, now, SQLite could not open the database, but i found it!
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();
}
So the correct pathseems to be :
string bpa = Application.dataPath + "/Raw/" + StaticDatas.databaseName;
----------------------------------------------------------------
UPDATE 4:
Ok, I think I know what the problem is (even if I don't understand it):
I can now get my datas from the streaming assets and copy it to the persistentDatapath, but when I create a connection with the file, SQLite throws an exception:
Could not open database file
Does someone know why?
---------------------------------------------------------------- UPDATE 5:
I do a Combine to create the path "targetPath", in the logs, it shows "/var/mobile/Container/Data/Application/manylettersanddigits/Documents/database.db"
However, in the SQLException, it shows the same but without the slash between Documents and database.db
Upvotes: 2
Views: 3089
Reputation: 2972
On IOS, the current path to streaming assets can be accessed with:
Application.dataPath + "/Raw/" + StaticDatas.databaseName;
IN THIS CASE, the "Could not open database file" was caused because I was creating 2 connection in the method (shame on me) Indeed, the second one was making it buggig (same variable for both), was a mistake from me.
Thanks to all.
Upvotes: 1