This may be dumb question but I couldn't find any solution on this. I need to know how to backup roomdatabase and retrieve it using Google Drive. Then what is the mime type for room database? Can anyone know how to do this? Let me post what I have tried so far:
private void saveFileToDrive(File file) {
mDriveClient = Drive.getDriveClient(this, GoogleSignIn.getLastSignedInAccount(this));
// Build a drive resource client.
mDriveResourceClient =
Drive.getDriveResourceClient(this, GoogleSignIn.getLastSignedInAccount(this));
final Task<DriveFolder> rootFolderTask =mDriveResourceClient.getRootFolder();
final Task<DriveContents> createContentsTask =mDriveResourceClient.createContents();
Tasks.whenAll(rootFolderTask, createContentsTask)
.continueWithTask(new Continuation<Void, Task<DriveFile>>() {
@Override
public Task<DriveFile> then(@NonNull Task<Void> task) throws Exception {
DriveFolder parent = rootFolderTask.getResult();
DriveContents contents = createContentsTask.getResult();
// OutputStream outputStream = contents.getOutputStream();
// try (Writer writer = new OutputStreamWriter(outputStream)) {
// writer.write("Hello World!");
// }
OutputStream oos = contents.getOutputStream();
if (oos != null) try {
InputStream is = new FileInputStream(file);
byte[] buf = new byte[4096];
int c;
while ((c = is.read(buf, 0, buf.length)) > 0) {
oos.write(buf, 0, c);
oos.flush();
}
}
finally { oos.close();}
MetadataChangeSet changeSet = new MetadataChangeSet.Builder()
.setTitle("mm.db")
.setMimeType("application/x-sqlite3")
.setStarred(true)
.build();
return mDriveResourceClient.createFile(parent, changeSet, contents);
}
})
.addOnSuccessListener(this,
new OnSuccessListener<DriveFile>() {
@Override
public void onSuccess(DriveFile driveFile) {
// showMessage(getString(R.string.file_created,
// driveFile.getDriveId().encodeToString()));
finish();
}
})
.addOnFailureListener(this, new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.e(TAG, "Unable to create file", e);
// showMessage(getString(R.string.file_create_error));
finish();
}
});
}
How to do backup for frequent time but using room database android? Thanks in advance.