问题
I need to push sqlite database file to phone app storage location. I tried this for my app package database.But not working.Is there any way?
I have tried using below command in device shell, but getting device not found message.
shell@android: adb push MY_DB_FILE /data/data/MY_PACKAGE_NAME/databases/
回答1:
Call the write() method shown below whenever you want your database file. It will create a backupname.db file in the root folder of your device.(you can change the name of the .db file in the backupDBPath string)
private void write() throws IOException {
File sd = Environment.getExternalStorageDirectory();
if (sd.canWrite()) {
String currentDBPath = DatabaseHelper.DATABASE_NAME;
String backupDBPath = "backupname.db";
File currentDB = new File(getDBPath(), currentDBPath);
File backupDB = new File(sd, backupDBPath);
if (currentDB.exists()) {
FileChannel src = new FileInputStream(currentDB).getChannel();
FileChannel dst = new FileOutputStream(backupDB).getChannel();
dst.transferFrom(src, 0, src.size());
src.close();
dst.close();
}
}
}
private String getDBPath() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
return getFilesDir().getAbsolutePath().replace("files", "databases") + File.separator;
} else {
return getFilesDir().getPath() + getPackageName() + "/databases/";
}
}
来源:https://stackoverflow.com/questions/27422062/push-sqlite-database-file-into-phone