ApplicationData.persistentDataPath Unity Editor for Android
Does anyone know where I should put the file (sqlite file) in the unity project directory structure so that after the apk is compiled and installed, the file stays saved in the persistentDataPath side on the Android side?
Thanks in advance!
+3
OCalero
source
to share
1 answer
Directly inside the Assets folder, create a folder named "StreamingAssets" and place your sqlite database file in that folder, and then use the following code to extract and copy the sqlite database to persistentDataPath:
void InitSqliteFile (string dbName)
{
// dbName = "example.sqlite";
pathDB = System.IO.Path.Combine (Application.persistentDataPath, dbName);
//original path
string sourcePath = System.IO.Path.Combine (Application.streamingAssetsPath, dbName);
//if DB does not exist in persistent data folder (folder "Documents" on iOS) or source DB is newer then copy it
if (!System.IO.File.Exists (pathDB) || (System.IO.File.GetLastWriteTimeUtc(sourcePath) > System.IO.File.GetLastWriteTimeUtc(pathDB))) {
if (sourcePath.Contains ("://")) {
// Android
WWW www = new WWW (sourcePath);
// Wait for download to complete - not pretty at all but easy hack for now
// and it would not take long since the data is on the local device.
while (!www.isDone) {;}
if (String.IsNullOrEmpty(www.error)) {
System.IO.File.WriteAllBytes(pathDB, www.bytes);
} else {
CanExQuery = false;
}
} else {
// Mac, Windows, Iphone
//validate the existens of the DB in the original folder (folder "streamingAssets")
if (System.IO.File.Exists (sourcePath)) {
//copy file - alle systems except Android
System.IO.File.Copy (sourcePath, pathDB, true);
} else {
CanExQuery = false;
Debug.Log ("ERROR: the file DB named " + dbName + " doesn't exist in the StreamingAssets Folder, please copy it there.");
}
}
}
}
+4
Mohanad S. Hamed
source
to share