- Home /
Copy database from Assets to Android sdcard?
I need to copy database from Assets to sdcard on the Android. Why this does not work:
if (!(File.Exists(Application.persistentDataPath+"/db.db")))
{
File.Copy(Application.dataPath+"/db.db",Application.persistentDataPath+"/db.db");
}
Answer by Bunny83 · Feb 09, 2013 at 05:10 PM
Well, have you checked that the file is even there? Unity packs all assets into it's own asset database format. You can't really access any assets via file access. I guess what you want has been described here. However read the documentation about the streamingAssetsPath carefully. On android you have no file access rights to your application folder unless you are on a "rooted" device. Furthermore, as you can read in the docs, all assets are packed into the apk file so Application.streamingAssetsPath will return an URL which can be used to load the files via the WWW class.
[Bunny83] Thank you very much, will try that. I have got rooted device and put file manually, does the Application.persistentDataPath has read/write ability? $$anonymous$$y main goal is to read/write sqlite database:
SqliteConnection con =new $$anonymous$$ono.Data.SqliteClient.SqliteConnection(connectionString);
I checked DLL loads, and file also exists at the path, under windows connection works but in android it does not work :(
Yes Application.persistentDataPath has read and write premission even on normal non-rooted devices. However Application.dataPath is read and write protected.
Application.persistentDataPath is also hidden from external access (on non rooted devices of course). Only the application itself can access it.
If you just want to initialize your database with some data, you might simply export your database as SQL and include this as textasset in your application. Now you can easily create a new database file in the persistentDataPath and just insert your data.
Answer by Compguru910 · May 28, 2013 at 08:09 PM
Also, you might want to check that it is not there on the phone as opposed to using the Windows Explorer. I was recently working on an app that wrote a file to the SD card. For some reason Windows Explorer didnt have access to some of the SD card folder (or internal storage). What I had to do was use a File Explorer app for Android to find the file.