How can I make a backup of the database to the sd card in android studio. I have the database made, but I do not know how to implement a backup and that is in the sd
How can I make a backup of the database to the sd card in android studio. I have the database made, but I do not know how to implement a backup and that is in the sd
You can implement the following method:
public void copiarDB(String nombreDB) {
try {
File sD = Environment.getExternalStorageDirectory();
File datos = Environment.getDataDirectory();
String rutaDbActual = "//data//" + getPackageName() + "//databases//" + nombreDB;
String rutaDbCopia = "copiadb.db";
if (sD.canWrite()) {
File dbActual = new File(datos, rutaDbActual);
File dbCopia = new File(sD, rutaDbCopia);
if (dbActual.exists()) {
FileChannel src = new FileInputStream(dbActual).getChannel();
FileChannel dst = new FileOutputStream(dbCopia).getChannel();
dst.transferFrom(src, 0, src.size());
src.close();
dst.close();
}
}
} catch (Exception e) {
System.out.print(e);
}
}
You will need to have permission WRITE_EXTERNAL_STORAGE
in the application