Sqlite数据库不会复制到真实设备的SD卡中

时间:2012-07-15 20:35:19

标签: java android

我通过这种方法将我的数据库(sqllite)复制到SD卡:

String currentDBPath =
                "\\data\\com.powergroupbd.tripmileage\\databases\\tripmileagedatabase";
                 String backupDBPath = "tripmileagedatabase";
                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();

这在模拟器中工作正常但是当我在真实设备中安装时,这并不显示SD卡中的任何文件。我缺少什么?

1 个答案:

答案 0 :(得分:1)

数据库的路径从/ data / data /开始。请注意,路径分隔符是正斜杠,而不是反斜杠。所以将代码更改为:

String currentDBPath =
            "/data/data/com.powergroupbd.tripmileage/databases/tripmileagedatabase";
             String backupDBPath = "tripmileagedatabase";
            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();