Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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/
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
409 views
Welcome To Ask or Share your Answers For Others

1 Answer

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/";
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...