There is a way to create and upload files to the official Google Docs and demo sources.
But there is no way to upload existing files.
I want to know how to upload a file (ex. Sqlite db file) of an Android app to Google Drive.
Thank you
There is a way to create and upload files to the official Google Docs and demo sources.
But there is no way to upload existing files.
I want to know how to upload a file (ex. Sqlite db file) of an Android app to Google Drive.
Thank you
You may want to check the Google Drive Android API on how to manage file content and metadata. You can create files in two ways with the Drive Android API: using the DriveClient.newCreateFileActivityIntentSender
method and the CreateFileActivityOptions
class; or using the DriveResourceClient.createFile
method.
You may also check these links for additional reference:
private fun handleSignInIntent(resultIntent: Intent) {
GoogleSignIn.getSignedInAccountFromIntent(resultIntent)
.addOnSuccessListener {
Toast.makeText(this, "Sign-in Successful!!", Toast.LENGTH_LONG).show()
val credentials = GoogleAccountCredential.usingOAuth2(
this,
Collections.singleton(DriveScopes.DRIVE_FILE)
)
credentials.selectedAccount = it.account
val drive =
Drive.Builder(AndroidHttp.newCompatibleTransport(), GsonFactory(), credentials)
.setApplicationName("DB BackUp").build()
driveServiceHelper=DriveServiceHelper(drive)
}
.addOnFailureListener {
Toast.makeText(this, "Sign-in Failed!!", Toast.LENGTH_LONG).show()
}
}
// Drive Service Helper class :
class DriveServiceHelper(private val driveService: Drive) {
private val executor = Executors.newSingleThreadExecutor()
fun createFile(filePath: String): Task<String> {
return Tasks.call(executor,object : Callable<String>{
override fun call(): String {
val fileMetadata=File()
fileMetadata.name="BACKUP_SAMPLE"
val fileToUpload = java.io.File(filePath)
val mediaContent = FileContent("*/*", fileToUpload)
var myFile = File()
try {
myFile=driveService.files().create(fileMetadata, mediaContent).execute()
} catch (e:Exception){
Log.e("DriveException>>", "${e.message} <<<")
}
if (myFile==null)
throw IOException("Null file received on Creation")
return myFile.id
}
})
}
}