【问题标题】:Android import database from external storageAndroid 从外部存储导入数据库
【发布时间】:2015-11-14 17:01:31
【问题描述】:

我正在尝试从外部驱动器 (USB) 导入数据库文件,以恢复备份数据。文件存在且路径正确,但没有导入数据库,这是我的代码:

File usb = new File("/storage/UsbDriveA/BackupHM/database.db");
File currentDB = getDatabasePath("database.db");

FileChannel source = null;
FileChannel destination = null;
String backupDBPath = "/storage/UsbDriveA/BackupHM/database.db";
File backupDB = new File(usb, backupDBPath);
                        try {
                            source = new FileInputStream(backupDB).getChannel();
                            destination = new FileOutputStream(currentDB).getChannel();
                            destination.transferFrom(source, 0, source.size());
                            source.close();
                            destination.close();
                            Toast toast1 = Toast.makeText(getApplicationContext(), ("Restore ok"), Toast.LENGTH_SHORT);
                            toast1.show();
                            finish();
                        } catch (IOException e) {
                            e.printStackTrace();
                            Toast toast = Toast.makeText(getApplicationContext(), ("Error!"), Toast.LENGTH_LONG);
                            toast.show();
                        }
                    }

【问题讨论】:

  • 你看到了什么失败?发布的代码是否会引发 IOException?我复制了你的代码并成功传输了一个文件。

标签: android sqlite import


【解决方案1】:

编码错误。将声明块更改为:

        //File usb = new File("/storage/UsbDriveA/BackupHM/database.db");
        File currentDB = getDatabasePath("database.db");

        FileChannel source = null;
        FileChannel destination = null;
        String backupDBPath = "/storage/UsbDriveA/BackupHM/database.db";
        File backupDB = new File(backupDBPath);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-10-29
    • 1970-01-01
    • 1970-01-01
    • 2016-05-01
    • 2020-12-15
    • 1970-01-01
    • 1970-01-01
    • 2013-05-22
    相关资源
    最近更新 更多