在android中导出数据库

时间:2016-01-29 07:13:09

标签: android sqlite

我正在使用此代码导出我的sqllite数据库。

 public void exportDB() {
        File sd = Environment.getExternalStorageDirectory();
        File data = Environment.getDataDirectory();
        FileChannel source = null;
        FileChannel destination = null;
        String backupDBPath = "contactsManager.db";
        String currentDBPath = "/data/" + "com.packagename"
                + "/databases/" + backupDBPath;
        System.out.println("oooooooooooooo" + currentDBPath);
        File currentDB = new File(data, currentDBPath);
        File backupDB = new File(sd, backupDBPath);
        try {
            source = new FileInputStream(currentDB).getChannel();
            destination = new FileOutputStream(backupDB).getChannel();
            destination.transferFrom(source, 0, source.size());
            source.close();
            destination.close();
             Toast.makeText(getApplicationContext(), "DB Exported!", Toast.LENGTH_LONG)
             .show();
            System.out.println("---------db exporeted");
        } catch (Exception e) {
            e.printStackTrace();
        }


    }

但我收到的文件未找到异常。这是日志。

java.io.FileNotFoundException:/data/data/packagename/databases/contactsManager.db:open failed:ENOENT(没有这样的文件或目录)

1 个答案:

答案 0 :(得分:0)

currentDB(源)不存在。检查你的路径。

还要确保目标路径与backupDB.mkdir()

一起存在