Perform a Blackup from the database to the sd [duplicate]

-1

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

    
asked by Erick Muñoz 27.06.2017 в 23:11
source

1 answer

0

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

    
answered by 27.06.2017 в 23:39