One of the worst things is to lose all your data when you change a phone or when you reset it. With this talk we'll see how to deal with this problem and how to secure your application's data.
most of the directories that are assigned to the app by the system: ▣ Shared preferences files ▣ Files in the directory returned by: □ getFilesDir() □ getDatabasePath(String) □ getExternalFilesDir(String) ▣ Files in directories created with getDir(String, int). Every temporary cached file is excluded from the backup
full-data backup by settings android:fullBackupOnly="true" in the Manifest ▣ On API 22 and lower, the app ignores this value and perform Key/Value Backup ▣ On API 23 and higher the app perform Auto Backup
of the SharedPreferences file static final String PREFS = "user_preferences"; // A key to uniquely identify the set of backup data static final String PREFS_BACKUP_KEY = "prefs"; // Allocate a helper and add it to the backup agent @Override public void onCreate() { SharedPreferencesBackupHelper helper = new SharedPreferencesBackupHelper(this, PREFS); addHelper(PREFS_BACKUP_KEY, helper); } }
= mContext.getDatabasePath(DATABASE_NAME).toString(); try { File dbFile = new File(inFileName); FileInputStream fis = new FileInputStream(dbFile); // Open the empty db as the output stream OutputStream output = new FileOutputStream(outFileName); // Transfer bytes from the input file to the output file byte[] buffer = new byte[1024]; int length; while ((length = fis.read(buffer)) > 0) { output.write(buffer, 0, length); } // Close the streams output.flush(); output.close(); fis.close(); Toast.makeText(mContext, "Backup Completed", Toast.LENGTH_SHORT).show(); } catch (Exception e) { Toast.makeText(mContext, "Unable to backup database. Retry", Toast.LENGTH_SHORT).show(); e.printStackTrace(); } }
try { File dbFile = new File(inFileName); FileInputStream fis = new FileInputStream(dbFile); // Open the empty db as the output stream OutputStream output = new FileOutputStream(outFileName); // Transfer bytes from the input file to the output file byte[] buffer = new byte[1024]; int length; while ((length = fis.read(buffer)) > 0) { output.write(buffer, 0, length); } // Close the streams output.flush(); output.close(); fis.close(); Toast.makeText(mContext, "Import Completed", Toast.LENGTH_SHORT).show(); } catch (Exception e) { Toast.makeText(mContext, "Unable to import database. Retry", Toast.LENGTH_SHORT).show(); e.printStackTrace(); } }
final int resultCode, final Intent data) { switch (requestCode) { case REQUEST_CODE_CREATOR: // Called after a file is saved to Drive. if (resultCode == RESULT_OK) { Log.i(TAG, "Backup successfully saved."); Toast.makeText(this, "Backup successufly loaded!", Toast.LENGTH_SHORT).show(); } break; case REQUEST_CODE_OPENER: if (resultCode == RESULT_OK) { DriveId driveId = data.getParcelableExtra(OpenFileActivityBuilder.EXTRA_RESPONSE_DRIVE_ID); DriveFile file = driveId.asDriveFile(); importFromDrive(file); } } }
@Override public void onResult(@NonNull DriveApi.DriveContentsResult driveContentsResult) { if (!driveContentsResult.getStatus().isSuccess()) { //deal with failure } // DriveContents object contains pointers to the actual byte stream DriveContents contents = driveContentsResult.getDriveContents(); try { ParcelFileDescriptor parcelFileDescriptor = contents.getParcelFileDescriptor(); FileInputStream fileInputStream = new FileInputStream(parcelFileDescriptor.getFileDescriptor()); // Open the empty db as the output stream OutputStream output = new FileOutputStream(inFileName); …. }