尝试将SQLite DB从数据复制到SD卡 [英] Trying to Copy SQLite DB from data to SD card

查看:157
本文介绍了尝试将SQLite DB从数据复制到SD卡的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下代码,发布在Stack Overflow的某处,并为了我的目的修改:

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


    }
}

我尝试访问它时是:

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

推荐答案

你在使用MY_DATABASE字面意思,当你可能实际想要使用它作为一个变量...

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.

这篇关于尝试将SQLite DB从数据复制到SD卡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆