是否可以在android中备份和恢复数据库文件?非根设备 [英] is it possible backup and RESTORE a database file in android? non root devices

查看:134
本文介绍了是否可以在android中备份和恢复数据库文件?非根设备的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中我需要备份我的数据库,
但是在我需要再次恢复之后,

in my app I need get a backup of my database, but after I'll need restore it again,

我已经阅读了一些内容,但是我不确定是否有必要使用root设备,
i需要备份/恢复非root设备中的所有数据,是否可能?

i have read somethings, but i do not sure if this is necessary to have a rooted device, i need backup/restore the all data in non root devices, is it possible?

my第一个想法是创建一个用于写入选择的txt文件,然后再插入它。

my first idea was creating a txt file for write the select, and later insert it again.

但我相信这是很多问题然后我不知道这是否可以复制数据库并粘贴到SD卡进行备份,并从SD卡复制并粘贴到数据库路径中以便还原非root设备。

but i believe this is much "problem" then i don't know if this is possible copy the database and paste in sd card for backup, and copy from sd card and paste in path of database for restore for non root devices.

推荐答案

以下是一些使其工作的代码

Here is some code to make it work

private void importDB() {
        try {
            File sd = Environment.getExternalStorageDirectory();
            File data = Environment.getDataDirectory();
                if (sd.canWrite()) {
                String currentDBPath = "//data//" + "<package name>"
                        + "//databases//" + "<database name>";
                String backupDBPath = "<backup db filename>"; // From SD directory.
                File backupDB = new File(data, currentDBPath);
                File currentDB = new File(sd, backupDBPath);

            FileChannel src = new FileInputStream(backupDB).getChannel();
            FileChannel dst = new FileOutputStream(currentDB).getChannel();
            dst.transferFrom(src, 0, src.size());
            src.close();
            dst.close();
            Toast.makeText(getApplicationContext(), "Import Successful!",
                    Toast.LENGTH_SHORT).show();

        }
    } catch (Exception e) {

        Toast.makeText(getApplicationContext(), "Import Failed!", Toast.LENGTH_SHORT)
                .show();

    }
}

private void exportDB() {
    try {
        File sd = Environment.getExternalStorageDirectory();
        File data = Environment.getDataDirectory();

        if (sd.canWrite()) {
            String currentDBPath = "//data//" + "<package name>"
                    + "//databases//" + "<db name>";
            String backupDBPath = "<destination>";
            File currentDB = new File(data, currentDBPath);
            File backupDB = new File(sd, backupDBPath);

            FileChannel src = new FileInputStream(currentDB).getChannel();
            FileChannel dst = new FileOutputStream(backupDB).getChannel();
            dst.transferFrom(src, 0, src.size());
            src.close();
            dst.close();
            Toast.makeText(getApplicationContext(), "Backup Successful!",
                    Toast.LENGTH_SHORT).show();

        }
    } catch (Exception e) {

        Toast.makeText(getApplicationContext(), "Backup Failed!", Toast.LENGTH_SHORT)
                .show();

    }
}

这篇关于是否可以在android中备份和恢复数据库文件?非根设备的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆