【发布时间】:2018-08-01 09:11:15
【问题描述】:
有一种方法可以创建文件并将其上传到官方 Google 文档和演示源。
但是没有办法上传现有文件。
我想知道如何将 Android 应用的文件(例如 Sqlite db 文件)上传到 Google Drive。
谢谢
【问题讨论】:
标签: android google-drive-android-api
有一种方法可以创建文件并将其上传到官方 Google 文档和演示源。
但是没有办法上传现有文件。
我想知道如何将 Android 应用的文件(例如 Sqlite db 文件)上传到 Google Drive。
谢谢
【问题讨论】:
标签: android google-drive-android-api
您可能需要查看Google Drive Android API,了解如何管理文件内容和元数据。您可以使用 Drive Android API 以两种方式创建文件:使用 DriveClient.newCreateFileActivityIntentSender 方法和 CreateFileActivityOptions 类;或使用DriveResourceClient.createFile 方法。
您还可以查看这些链接以获取更多参考:
【讨论】:
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
}
})
}
}
【讨论】: