Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public void backUp() {
- try {
- File sd = Environment.getExternalStorageDirectory();
- File data = Environment.getDataDirectory();
- if (sd.canWrite()) {
- String currentDBPath = "//data//your package name//databases//dbname.db";
- String backupDBPath = "dbname.db";
- File currentDB = new File(data, currentDBPath);
- File backupDB = new File(sd, backupDBPath);
- Log.d("backupDB path", "" + backupDB.getAbsolutePath());
- 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();
- Toast.makeText(getApplicationContext(), "Backup is successful to SD card", Toast.LENGTH_SHORT).show();
- }
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- public void restore() {
- try {
- File sd = Environment.getExternalStorageDirectory();
- File data = Environment.getDataDirectory();
- if (sd.canWrite()) {
- String currentDBPath = "//data//your package name//databases//dbname.db";;
- String backupDBPath = "dbname.db";
- File currentDB = new File(data, currentDBPath);
- File backupDB = new File(sd, backupDBPath);
- if (currentDB.exists()) {
- FileChannel src = new FileInputStream(backupDB).getChannel();
- FileChannel dst = new FileOutputStream(currentDB).getChannel();
- dst.transferFrom(src, 0, src.size());
- src.close();
- dst.close();
- Toast.makeText(getApplicationContext(), "Database Restored successfully", Toast.LENGTH_SHORT).show();
- }
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement