Я пытаюсь синхронизировать базу данных комнаты с Google Drive, и я проследил за этим документом https://medium.com/glucosio-project/backup-restore-a-realm-database-on-google-drive-with-drive-api-c238515a5975, но я не могу синхронизировать базу данных на диске.
Я также попытался https://stackoverflow.com/a/49297966/8143436 ответить на этот вопрос, а также попытаться синхронизировать базу данных, как показано ниже:
Я попробовал ниже:
private fun singIn() {
val requiredScopes = HashSet<Scope>(2)
requiredScopes.add(Drive.SCOPE_FILE)
val signInAccount = GoogleSignIn.getLastSignedInAccount(activity!!)
if (signInAccount != null && signInAccount.grantedScopes.containsAll(requiredScopes)) {
initializeDriveClient(signInAccount)
} else {
val signInOptions = GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestScopes(Drive.SCOPE_FILE)
.build()
val googleSignInClient = GoogleSignIn.getClient(activity!!, signInOptions)
startActivityForResult(googleSignInClient.signInIntent, REQUEST_CODE_SIGN_IN)
}
}
private fun initializeDriveClient(signInAccount: GoogleSignInAccount) {
driveClient = Drive.getDriveClient(activity!!, signInAccount)
driveResourceClient = Drive.getDriveResourceClient(activity!!, signInAccount)
onDriveClientReady()
}
private fun onDriveClientReady() {
getRootFolder()
}
private fun getRootFolder() {
/* Get the app folder */
val appFolderTask = driveResourceClient!!.rootFolder
appFolderTask.addOnSuccessListener {
baseFolder = it
checkForBackUp()
}
appFolderTask.addOnFailureListener {
}
}
private fun checkForBackUp() {
val query = Query.Builder()
.addFilter(Filters.eq(SearchableField.TITLE, BACK_UP))
.build()
val queryTask = baseFolder?.let { driveResourceClient?.queryChildren(it, query) }
queryTask!!.addOnSuccessListener(activity!!, object : OnSuccessListener<MetadataBuffer> {
override fun onSuccess(metadataBuffer: MetadataBuffer?) {
if (metadataBuffer?.count == 0) {
Log.d("TTTT", "File $BACK_UP not found\n")
/* Make file backup */
backUpDatabase()
} else {
Log.d("TTTT", metadataBuffer?.count.toString() + " Instances of file " + BACK_UP + " found\n")
val metadata = metadataBuffer?.get(metadataBuffer.count - 1)
restoreBackUp(metadata?.driveId!!.asDriveFile())
}
}
})
}
private fun restoreBackUp(asDriveFile: DriveFile?) {
// if (!dbShm.exists()) {
// Log.d("Hello", "Local database not found?!\\n")
// return
// }
val dbFileOld = activity!!.getDatabasePath("my_db")
/* Check of dbFileExists on device, delete if it does because it needs to be completely over written */
if (dbFileOld.exists()) {
dbFileOld.delete()
}
val dbFileNew = activity!!.getDatabasePath("my_db")
val fileOutputStream = FileOutputStream(dbFileNew)
val openFileTask = driveResourceClient?.openFile(asDriveFile!!, DriveFile.MODE_READ_ONLY);
openFileTask?.continueWith { p0 ->
val backupContents = p0.result
val inputStream = backupContents!!.inputStream
Log.d("TTTT", "Attempting to restore from database\n")
val buffer = ByteArray(1024)
var c: Int = inputStream.read(buffer, 0, buffer.size)
while (c != -1 && c > 0) {
fileOutputStream.write(buffer, 0, c)
c = inputStream.read(buffer, 0, buffer.size)
}
fileOutputStream.flush()
fileOutputStream.close()
fileOutputStream.flush()
fileOutputStream.close()
Log.d("TTTT", "Database restored\n")
driveResourceClient!!.discardContents(backupContents)
}?.addOnFailureListener {
Log.d("TTTT", "Could not read file contents\\n")
}
}
private fun backUpDatabase() {
val db = activity!!.getDatabasePath("my_db")
if (!db.exists()) {
Log.d("TTTT", "Local database not found?!\\n")
return
}
val fileInputStream = FileInputStream(db)
val createContentsTask = driveResourceClient?.createContents()
Log.d("TTTT", "Creating a back-up of the Database File\n")
Tasks.whenAll(createContentsTask).continueWithTask {
val contents = createContentsTask?.result
val outputStream = contents?.outputStream
Log.d("TTTT", "Attempting to write\\n")
val buffer = ByteArray(1024)
var c: Int = fileInputStream.read(buffer)
while (c != -1) {
outputStream?.write(buffer, 0, c)
c = fileInputStream.read(buffer)
}
outputStream!!.flush()
outputStream.close()
fileInputStream.close()
Log.d("TTTT", "Database written\\n")
val changeSet = MetadataChangeSet.Builder()
.setTitle(BACK_UP)
.setMimeType("application/x-sqlite3")
.setStarred(false)
.build()
driveResourceClient!!.createFile(baseFolder!!, changeSet, contents)
}.addOnSuccessListener {
Log.d("TTTT", "Back up file created\n")
}.addOnFailureListener {
Log.d("TTTT", "Could not create back up file\n")
}
}
возможно ли синхронизировать базу данных комнат на диске Google?