Uma das formas de se gerar backup do banco de dados do seu aplicativo é utilizando os comandos abaixo.
public void gerarBackup(Boolean silent) { try { File sd = Environment.getExternalStorageDirectory(); File data = Environment.getDataDirectory(); if (sd.canWrite()) { createDirIfNotExists("/bkps"); String currentDBPath = "//data//br.com.j2v.app.nomeAplicativo//databases//dbNAME"; String backupDBPath = "bkps/dbNAME-" + getDateTime("log") + ".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(); if (!silent) { Toast.makeText(getBaseContext(), "Backup gerado com sucesso!\r\nCaminho: " + backupDBPath, Toast.LENGTH_LONG).show(); } } } catch (Exception e) { Log.e("J2V", e.toString()); Toast.makeText(getBaseContext(), e.toString(), Toast.LENGTH_LONG).show(); } }
Deixe um comentário