In android, I'd like to include a database at the time of installation i.e. in the .apk file. Is it possible to do that? I have some data in a database that I would want to use in the app. I don't want to use OnCreate method to create a database. So, where should I keep my db file so that it is a开发者_开发知识库ccessible to the app after installation?
create your database file, include it in your assets directory and on first launch of your application copy it to /data/data/PACKAGE_NAME/databases/
This website helped me out greatly.
http://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/
I create my SQLite databases separately, add them to the assets directory in the app package, and then on launch of the app copy the databases into the /data/data/com.company.appname/databases/ directory for use there.
I also check to see if the file already exists before bothering to copy it over (so I generally only copy it on first ever launch).
eg
boolean createDB= false;
File dbDir = new File(DB_PATH);
File dbFile = new File(DB_PATH + DB_NAME);
if(!dbDir.exists())
{
dbDir.mkdir();
createDB = true;
}
else if (!dbFile.exists())
{
createDB = true;
}
精彩评论