0

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

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
HUJIN SHIN
  • 63
  • 6

2 Answers2

0

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:

abielita
  • 13,147
  • 2
  • 17
  • 59
  • Note that as of December 2018, the Google Drive Android API is in the process of being deprecated by Google. – shagberg Dec 17 '18 at 22:36
-1
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
            }
        })
    }
}
  • Instead of posting just a link, post an explanation here. So, even if the link becomes available in the future, people could still refer to your answer. – kk. Sep 17 '20 at 15:02
  • please remove your downvote I've added the code – Sameep Sharma Dec 17 '20 at 13:23