Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 我想通过代码将数据库导出到sd卡,但它不会在sd卡中创建db文件_Java_Android Studio - Fatal编程技术网

Java 我想通过代码将数据库导出到sd卡,但它不会在sd卡中创建db文件

Java 我想通过代码将数据库导出到sd卡,但它不会在sd卡中创建db文件,java,android-studio,Java,Android Studio,请帮帮我,我能做什么。 我想通过代码将数据库导出到sd卡,但它不会在sd卡中创建db文件 我试过的代码 // error in getting the " File dir = new File(getStorageBasedirectory().getAbsolutePath+ "/backup");" public File getBackupData`enter code here`baseFile() { File dir = new File(Environment

请帮帮我,我能做什么。 我想通过代码将数据库导出到sd卡,但它不会在sd卡中创建db文件

我试过的代码

 // error in getting the " File dir = new File(getStorageBasedirectory().getAbsolutePath+ "/backup");"
 public File getBackupData`enter code here`baseFile() {
        File dir = new File(Environment.getExternalStorageDirectory()+ "/backup");
        if (!dir.exists()) {
            dir.mkdirs();
        }
        return new File(dir, DATABASE_NAME);
    }
    public final boolean backupDatabase() {
        File from = context.getDatabasePath(DATABASE_NAME);
        File to = this.getBackupDatabaseFile();
        try {
            FileUtils.copyFile(from, to);
            return true;
        } catch (IOException e) {
            // TODO Auto-generated catch block
            Toast.makeText(context, "Error while copying file", Toast.LENGTH_SHORT).show();
        }
        return false;
    }
    public static void copyFile(File src, File dst) throws IOException {
        FileInputStream in = new FileInputStream(src);
        FileOutputStream out = new FileOutputStream(dst);
        FileChannel fromChannel = null, toChannel = null;
        try {
            fromChannel = in.getChannel();
            toChannel = out.getChannel();
            fromChannel.transferTo(0, fromChannel.size(), toChannel);
        } finally {
            if (fromChannel != null)
                fromChannel.close();
            if (toChannel != null)
                toChannel.close();
        }
    }
试试这个代码

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

    if (sd.canWrite()) {
        String currentDBPath = "//data//{package name}//databases//{database name}";
        String backupDBPath = "{database name}";
        File currentDB = new File(data, 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();
        }
    }
} catch (Exception e) {
}

你能把你的密码寄出去吗