61

SQLiteデータベースをSD カードに自動的にバックアップする機能を Android アプリに追加したいと考えています。

これについて最善の方法は何ですか?利用可能な例やチュートリアルはありますか?

4

10 に答える 10

123

このコードは私のために働きます!

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

        if (sd.canWrite()) {
            String currentDBPath = "//data//{package name}//databases//{database name}";
            String backupDBPath = "{database name}";
            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) {
    }

これがルート以外の電話で機能するかどうかは誰にもわかりませんか? ルート化されたG1でのみ試しました。

于 2010-04-18T10:19:10.443 に答える
21
try {
    File sd = Environment.getExternalStorageDirectory();
    File data = Environment.getDataDirectory();

    if (sd.canWrite()) {
        String currentDBPath = "//data//"+ packageName +"//databases//"+dbList[0];
        String backupDBPath = dbList[0];
        File currentDB = new File(data, currentDBPath);
        File backupDB = new File(sd, backupDBPath);

        FileChannel src = new FileInputStream(currentDB).getChannel();
        FileChannel dst = new FileOutputStream(backupDB).getChannel();
        dst.transferFrom(src, 0, src.size());
        src.close();
        dst.close();
        Toast.makeText(getBaseContext(), backupDB.toString(), Toast.LENGTH_LONG).show();
    }
} catch (Exception e) {
    Toast.makeText(getBaseContext(), e.toString(), Toast.LENGTH_LONG).show();
}

これは、「/」が「\」である上記の例とは対照的に機能しますが、それを理解するのに私の人生の 20 分を無駄にしましたが、もっと早くそれを見るべきでした。はToast、ファイルがどこに置かれたか、または機能しない場合に何が問題なのかを教えてくれます。

于 2012-01-18T00:23:00.477 に答える
10

SQLite データベースは完全に自己完結型のファイルであり、移植可能です。ファイル全体を直接 SD カードにコピーするだけです。

最初に、SDカードがデバイスにインストールされているかどうか、およびそのパスが何であるかを確認します(を使用Environment.getExternalStorageDirectory())。

于 2010-01-03T17:21:31.283 に答える
3

これに似た質問に、 に配置できる方法で答えSQLiteOpenHelperました。ある種の外部ストレージから内部アプリケーション ストレージに db ファイルをコピーするのと同じくらい簡単です。db ファイルを開いて読み取り、Android がデータベース呼び出しを行うのに適切な状態であることを確認する追加のコードもあります。

于 2011-06-30T23:38:21.947 に答える
2

アプリケーションで許可を与える必要がありandroid.permission.WRITE_EXTERNAL_STORAGEます。ルート化されていないデバイスでは問題なく動作します。

于 2012-07-27T07:42:00.773 に答える
1

これに慣れていない場合は、データベースアダプターでデータベース名を見つけます。

これは SharedPreferences に対しても実行できますが、Context.MODE_PRIVATE を Context.MODE_MULTI_PROCESS に変更することに注意してください。

SharedPreferences_name は次のようになります =ExportSP("temp.xml");

String currentPathForSharedPreferences = "/data/"+ context.getPackageName() +"/shared_prefs/"+ SharedPreferences_name;

輸出用

exportDB("MyDbName");

private void exportDB(String db_name){

File sd = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + 
                File.separator + "Your Backup Folder"+ 
                File.separator );

          boolean success = true;
           if (!sd.exists()) {
               success = sd.mkdir();
           }
           if (success) {

        File data = Environment.getDataDirectory();
       FileChannel source=null;
       FileChannel destination=null;
       String currentDBPath = "/data/"+ context.getPackageName() +"/databases/"+db_name;
       String backupDBPath = db_name;
       File currentDB = new File(data, currentDBPath);
       File backupDB = new File(sd, backupDBPath);
       try {
            source = new FileInputStream(currentDB).getChannel();
            destination = new FileOutputStream(backupDB).getChannel();
            destination.transferFrom(source, 0, source.size());
            source.close();
            destination.close();
            Toast.makeText(this, "Please wait", Toast.LENGTH_SHORT).show();
        } catch(IOException e) {
            e.printStackTrace();
        }
           }}

輸入用

importDB("MyDbName");

private void importDB(String db_name){
        File sd = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + 
                File.separator + "Your Backup Folder"+ 
                File.separator );
        File data = Environment.getDataDirectory();
       FileChannel source=null;
       FileChannel destination=null;
       String backupDBPath = "/data/"+ context.getPackageName() +"/databases/"+db_name;
       String currentDBPath = db_name;
       File currentDB = new File(sd, currentDBPath);
       File backupDB = new File(data, backupDBPath);
       try {
            source = new FileInputStream(currentDB).getChannel();
            destination = new FileOutputStream(backupDB).getChannel();
            destination.transferFrom(source, 0, source.size());
            source.close();
            destination.close();
            Toast.makeText(this, "Please wait", Toast.LENGTH_SHORT).show();
        } catch(IOException e) {
            e.printStackTrace();
        }
}
于 2016-05-01T17:46:18.110 に答える
1

電話がルート化されているかどうかはわかりませんが、ファイルを次の場所に書き込む必要があります。

/Android/data/{package_name}/files/

これは、ルート化されているかどうかに関係なく機能します。

于 2010-09-22T03:03:41.260 に答える
-1
@Override
protected void onCreate(Bundle savedInstanceState) {
    try {
        File sd = Environment.getExternalStorageDirectory();
        File data = Environment.getDataDirectory();

        if (sd.canWrite()) {
            String currentDBPath = "//data//"+getPackageName()+"//databases//"+DATABASE_NAME+"";
            String backupDBPath = "backup.db";
            File currentDB = new File(data, currentDBPath);
            File backupDB = new File(sd, backupDBPath);

            FileChannel src = new FileInputStream(currentDB).getChannel();
            FileChannel dst = new FileOutputStream(backupDB).getChannel();
            dst.transferFrom(src, 0, src.size());
            src.close();
            dst.close();
            Toast.makeText(getBaseContext(), backupDB.toString(), Toast.LENGTH_LONG).show();
        }
    } catch (Exception e) {
        Toast.makeText(getBaseContext(), e.toString(), Toast.LENGTH_LONG).show();
    }
}
于 2017-05-01T13:03:03.903 に答える