SQLiteデータベースをSD カードに自動的にバックアップする機能を Android アプリに追加したいと考えています。
これについて最善の方法は何ですか?利用可能な例やチュートリアルはありますか?
このコードは私のために働きます!
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でのみ試しました。
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
、ファイルがどこに置かれたか、または機能しない場合に何が問題なのかを教えてくれます。
SQLite データベースは完全に自己完結型のファイルであり、移植可能です。ファイル全体を直接 SD カードにコピーするだけです。
最初に、SDカードがデバイスにインストールされているかどうか、およびそのパスが何であるかを確認します(を使用Environment.getExternalStorageDirectory()
)。
これに似た質問に、 に配置できる方法で答えSQLiteOpenHelper
ました。ある種の外部ストレージから内部アプリケーション ストレージに db ファイルをコピーするのと同じくらい簡単です。db ファイルを開いて読み取り、Android がデータベース呼び出しを行うのに適切な状態であることを確認する追加のコードもあります。
アプリケーションで許可を与える必要がありandroid.permission.WRITE_EXTERNAL_STORAGE
ます。ルート化されていないデバイスでは問題なく動作します。
これに慣れていない場合は、データベースアダプターでデータベース名を見つけます。
これは 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();
}
}
電話がルート化されているかどうかはわかりませんが、ファイルを次の場所に書き込む必要があります。
/Android/data/{package_name}/files/
これは、ルート化されているかどうかに関係なく機能します。
@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();
}
}