0

Possible Duplicate:
Moving my db to sd card not working

I'm trying to save a sqlite file to my sdcard. I'm using the code from this question

 try {
    File sd = Environment.getExternalStorageDirectory();
    File data = Environment.getDataDirectory();

    if (sd.canWrite()) {
        String currentDBPath = "\\data\\com.test.mytest\\databases\\test_db";
        String backupDBPath = "test_db";
        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();
        }
    }
} catch (Exception e) {
}

This is a very highly rated answer, so I would think it should work pretty simply. I get no errors in logcat. I don't see any created directory/created files. I also have the "write to external" permission in my manifest.

4

2 回答 2

1

文件 sd = Environment.getExternalStorageDirectory(); 文件数据 = Environment.getDataDirectory();

         if (sd.canWrite()) {
             String currentDBPath = "/data/packagename/databases/DATABASENAME";
             String backupDBPath = "/backup/"+"main.db";
             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();
             } else {
                 Log.e(TAG, "File does not exist: " + currentDBPath);
             }
于 2012-11-20T09:31:42.927 回答
0

不要忘记在 amnifest 文件中声明权限

使用权限 android:name="android.permission.WRITE_EXTERNAL_STORAGE"

于 2012-11-20T09:27:20.223 回答