push sqlite database file into phone

时光毁灭记忆、已成空白 提交于 2019-12-13 11:23:39

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!