Question

I need to copy my database from Assets folder to "/data/data/com.example.provadatabase/databases/"

i was using this code

private static String DB_PATH = "/data/data/com.example.provadatabase/databases/";

private static String DB_NAME = "MN1";

and then create a database if not exist then it should copy into the created database the database from assets

InputStream myInput = myContext.getAssets().open(DB_NAME);

String outFileName = DB_PATH + DB_NAME;

OutputStream myOutput = new FileOutputStream(outFileName);

byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer))>0){
myOutput.write(buffer, 0, length);
}

myOutput.flush();
myOutput.close();
myInput.close();

}

The database is well created but it doesen't copy my database to databases folder and the code give this warning "Do not hardcode "/data/"; use Context.getFilesDir().getPath() instead"

What should i use to copy my db?

Was it helpful?

Solution

Exactly what the error tells you to do.

String outFileName = context.getDatabasePath(DatabaseHelper.DB_NAME).getParentFile() + DB_NAME;

This assumes that context is available, which it usually is in the constructor. But honestly, I would just use SQLiteAssetHelper.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top