Android 尝试将SQLite DB从数据复制到SD卡

Android 尝试将SQLite DB从数据复制到SD卡,android,sqlite,copy,android-sdcard,Android,Sqlite,Copy,Android Sdcard,我正在使用以下代码,这些代码发布在堆栈溢出的某个位置,并根据我的目的进行了修改: try { File sd = Environment.getExternalStorageDirectory(); File data = Environment.getDataDirectory(); if (sd.canWrite()) { String currentDBPath = "//data//"+ "com.exercise

我正在使用以下代码,这些代码发布在堆栈溢出的某个位置,并根据我的目的进行了修改:

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

        if (sd.canWrite()) {
            String currentDBPath = "//data//"+ "com.exercise.AndroidSQLite" +"//databases//"+"MY_DATABASE";
            String backupDBPath = "/temp/MY_DATABASE";
            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(getBaseContext(), backupDB.toString(), Toast.LENGTH_LONG).show();

        }
    } catch (Exception e) {

        Toast.makeText(getBaseContext(), e.toString(), Toast.LENGTH_LONG).show();


    }
}
因此,当我尝试访问它时,我得到的错误是:

java.io.FileNotFoundException: /data/data/com.exercise.AndroidSQLite/databases/MY_DATABASE: open failed: EACCES (Permission Denied)
我正在尝试复制此文件而不在平板电脑上生根。在应用程序中设置写入外部存储目录权限;我就是绕不开这个错误。如果您能帮我解决这个问题,我会很感激的,这让我发疯的

您正在使用“MY_数据库”,而实际上您可能想将其用作变量


删除它周围的引号,看看这是否解决不了您的问题。

我正在用android应用程序备份我的数据库,它工作正常。如果您是数据库文件的所有者,则只能访问该文件,这意味着您的应用程序创建了该文件

我认为你的路径是错误的,我的应用程序中有:

private static final String DATABASE_NAME = "my.db.name";

public File getBackupDatabaseFile() {
    File dir = new File(getStorageBaseDirectory().getAbsolutePath() + "/backup");
    if (!dir.exists()) {
        dir.mkdirs();
    }
    return new File(dir, DATABASE_NAME);
}
public final boolean backupDatabase() {
    File from = mContext.getDatabasePath(DATABASE_NAME);
    File to = this.getBackupDatabaseFile();
    try {
        FileUtils.copyFile(from, to);
        return true;
    } catch (IOException e) {
        // TODO Auto-generated catch block
       Log.e(TAG, "Error backuping up database: " + e.getMessage(), e);
    }
    return false;
}
FileUtils.copyFIle是这样的:

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();
    }
}

MY_数据库不是一个变量;它是在另一个应用程序中创建的数据库的名称,该应用程序很快被C+P组合在一起,因此我可以尝试查看它。我不知道如何通过其他文件访问它。。。我的一大疏忽。谢谢。实际上,不需要手动提取到数据库路径,如果这样做,LINT会警告您。您应该使用此方法。