Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/222.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
android将数据库从内部存储复制到外部存储返回空数据库_Android_Database - Fatal编程技术网

android将数据库从内部存储复制到外部存储返回空数据库

android将数据库从内部存储复制到外部存储返回空数据库,android,database,Android,Database,我试图将数据库从内部存储复制到外部存储,但它没有复制数据库。它只是创建一个新的空sqlite文件。你知道我到底错过了什么吗 以下是我正在使用的: public static void copyFile(String currentDBPath, String backupDBPath){ try { File sd = Environment.getExternalStorageDirectory(); //File data = Environm

我试图将数据库从内部存储复制到外部存储,但它没有复制数据库。它只是创建一个新的空sqlite文件。你知道我到底错过了什么吗

以下是我正在使用的:

    public static void copyFile(String currentDBPath, String backupDBPath){
    try {
        File sd = Environment.getExternalStorageDirectory();
        //File data = Environment.getDataDirectory();

        if (sd.canWrite()) {
            File currentDB = new File(currentDBPath);
            File backupDB = new File(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) {
        e.printStackTrace();
    }
}
然后像这样使用它:

File database = new File("/sdcard/Stam/Data/");
        database.mkdirs();
        RPCCommunicator.copyFile("/data/data/" + this.getPackageName()+ "/databases/stam_sys_tpl.sqlite","/sdcard/Stam/Data/system.sqlite");
因此,使用它创建system.sqlite,但它是空的,没有任何表。我想做的事情是将数据库从当前目录移动到新目录,而不丢失任何数据


知道它为什么不能正常工作吗?

您无法将数据库文件复制到外部存储

您可以在外部存储器中创建新数据库


查询内部数据库并插入您创建的新数据库

使用
Context.getDataBasePath()
获取数据库文件

您确定扩展名是.sqlite而不是.db吗?通常最好不要假设外部存储名为“/sdcard”-始终使用环境35; getExternalStorageDirectory()。另外,不要手动构造数据库文件的路径,请使用Context#getDatabasePath(“stam#u sys_tpl.sqlite”)。好的,我刚刚尝试以您的方式使用它……仍然在创建一个新的空文件。是否可以将数据库文件复制到sd卡而不丢失任何数据。这才是我真正需要的。我可以从我的资产文件夹复制一个空的,但我需要保留用户数据。
dst.transferFrom(src,0,src.size())的输出是什么,它返回复制的字节数-它是否与实际大小匹配?否则,请使用普通的InputStream来输出流复制循环..是否在复制之前关闭用于创建数据库的
SQLiteOpenHelper
?听起来很奇怪。在安卓2.3上运行良好,只需简单的表格和直接复制。