我试图从我的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
团结: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(两个变量都相同),这是我的错误。

谢谢大家。

08-27 22:20