Trying to Copy SQLite DB from data to SD card

我只是一个虾纸丫 提交于 2019-12-07 12:03:43

问题


I am using the following code that was posted somewhere on Stack Overflow and modified for my purposes:

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


    }
}

So the error I get when I try to access it is:

java.io.FileNotFoundException: /data/data/com.exercise.AndroidSQLite/databases/MY_DATABASE: open failed: EACCES (Permission Denied)

I am trying to copy this file without rooting my tablet. The write external storage directory permission is set in the application; I just can't get around this error. Would appreciate help on resolving this issue, it's driving me mad


回答1:


You are using "MY_DATABASE" literally when you probably actually want to use it as a variable...

Remove the quotes from around it and see if that doesn't solve your problem.




回答2:


I am backing up my database in my android application and it works fine. You can only access the database file if you are the owner of it, meaning your application created it.

I think your path is wrong, I have this in my app:

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

And FileUtils.copyFIle is this:

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


来源:https://stackoverflow.com/questions/11043175/trying-to-copy-sqlite-db-from-data-to-sd-card

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!