5

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.

abielita
  • 13,147
  • 2
  • 17
  • 59
M.Yogeshwaran
  • 1,419
  • 4
  • 22
  • 48

1 Answers1

0

You may refer with this thread. There might be a reliability issue because Google Drive is a separate app from your own. Drive should only be working with the file when your app is not actively using the database. I do not know if Drive is taking any steps to ensure this. Otherwise, you could get data corruption. You will also get data corruption if the user modifies the data on multiple devices before Drive has done any sync work (e.g., one of the devices is presently offline). It also stated that SQLite needs direct filesystem access to the database. Room does not change that.

abielita
  • 13,147
  • 2
  • 17
  • 59