Advertisement
Guest User

Untitled

a guest
Sep 29th, 2016
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.93 KB | None | 0 0
  1. public void backUp() {
  2. try {
  3. File sd = Environment.getExternalStorageDirectory();
  4. File data = Environment.getDataDirectory();
  5.  
  6. if (sd.canWrite()) {
  7. String currentDBPath = "//data//your package name//databases//dbname.db";
  8. String backupDBPath = "dbname.db";
  9.  
  10. File currentDB = new File(data, currentDBPath);
  11. File backupDB = new File(sd, backupDBPath);
  12.  
  13. Log.d("backupDB path", "" + backupDB.getAbsolutePath());
  14.  
  15. if (currentDB.exists()) {
  16. FileChannel src = new FileInputStream(currentDB).getChannel();
  17. FileChannel dst = new FileOutputStream(backupDB).getChannel();
  18. dst.transferFrom(src, 0, src.size());
  19. src.close();
  20. dst.close();
  21. Toast.makeText(getApplicationContext(), "Backup is successful to SD card", Toast.LENGTH_SHORT).show();
  22. }
  23. }
  24. } catch (Exception e) {
  25. e.printStackTrace();
  26. }
  27. }
  28.  
  29. public void restore() {
  30. try {
  31. File sd = Environment.getExternalStorageDirectory();
  32. File data = Environment.getDataDirectory();
  33.  
  34. if (sd.canWrite()) {
  35. String currentDBPath = "//data//your package name//databases//dbname.db";;
  36. String backupDBPath = "dbname.db";
  37. File currentDB = new File(data, currentDBPath);
  38. File backupDB = new File(sd, backupDBPath);
  39.  
  40. if (currentDB.exists()) {
  41. FileChannel src = new FileInputStream(backupDB).getChannel();
  42. FileChannel dst = new FileOutputStream(currentDB).getChannel();
  43. dst.transferFrom(src, 0, src.size());
  44. src.close();
  45. dst.close();
  46. Toast.makeText(getApplicationContext(), "Database Restored successfully", Toast.LENGTH_SHORT).show();
  47. }
  48. }
  49. } catch (Exception e) {
  50. e.printStackTrace();
  51. }
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement