0

重複の可能性:
データベースをSDカードに移動できない

sqlite ファイルを SD カードに保存しようとしています。この質問のコードを使用しています

 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) {
}

これは非常に評価の高い回答なので、かなり簡単に機能するはずです。logcat にエラーはありません。作成されたディレクトリ/作成されたファイルが表示されません。マニフェストに「外部への書き込み」権限もあります。

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 ファイルで許可を宣言することを忘れないでください

uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"

于 2012-11-20T09:27:20.223 に答える