0

我通过这种方法将我的数据库(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 卡中的任何文件。我错过了什么?

4

1 回答 1

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();
于 2012-07-15T20:57:52.463 回答