Why you want to put the database file direct into the data/data folder? For testing purposes? Don't make sense to me, I would choose one of the options below:
- Load data from the server and insert it into the database.
- Put the database file in the assets folder, and then copy the bytes to create the app database (you can't move the database file).
Example (as requested):
First you need to put your database file inside the assets/database folder. If your database is bigger than 1mb you need to split it. (i.e. database1, database2, database3...)
You probably have a class to do all database tasks, here I have a class called DatabaseHelper (extends SQLiteOpenHelper). Don't forget to set you database path and name.
private static String DB_PATH = "/data/data/com.example/databases/";
private static String DB_NAME = "mydb.sqlite";
If you had to split your database, declare the pieces into a variable too.
private static String[] sFiles = {'database1','database2','database3'};
You can create/open your database quickly using the getReadableDatabase() method.
Then, create an implementation like this:
private void copyDatabase() throws IOException {
// Path to the empty database.
String outFilename = DB_PATH + DB_NAME;
// Open the empty database as the output stream.
OutputStream outputDatabase = new FileOutputStream(outFilename);
// Transfer bytes from the input file to the output file.
byte[] buffer = new byte[1024];
for (int i = 0; i < sFiles.length; ++i) {
// Open the local database as the input stream.
InputStream is = mContext.getAssets().open("database/" + sFiles[i]);
int length;
while ((length = is.read(buffer)) > 0) {
outputDatabase.write(buffer, 0, length);
}
// Closing stream.
is.close();
}
// Closing the streams.
outputDatabase.flush();
outputDatabase.close();
// Closing database.
close();
}