Android Google Drive Api Query App Folder By Filename

May 22, 2018
val appFolderTask = driveResourceClient.appFolder

appFolderTask.continueWithTask { task ->
    val parent = task.result
    val query = Query.Builder()
            .addFilter(Filters.eq(SearchableField.TITLE, "backup-latest.db"))
            .build()
    driveResourceClient.queryChildren(parent, query)
}.addOnSuccessListener { meta ->
    if (meta.count > 0) {
        val fileMeta = meta.get(0)
    }
    else {

    }
}.continueWithTask { task ->
    val meta = task.result
    if (meta.count == 0)   {
        return@continueWithTask null
    }

    val fileMeta = meta.get(0)
    val file = fileMeta.driveId.asDriveFile()
    driveResourceClient.openFile(file, DriveFile.MODE_READ_ONLY)
}.addOnSuccessListener { driveContents ->
    val tempInputStream = driveContents.inputStream
    val inputStream = tempInputStream.copyToByteArrayStream()

    val outputStream = ByteArrayOutputStream()

    inputStream.use { input ->
        outputStream.use { output ->
            input.copyTo(output)
        }
    }

    val restoreData = String(outputStream.toByteArray(), Charsets.UTF_8)

    // use this to free resources?
    driveResourceClient.discardContents(driveContents)
}
This work is licensed under a
Creative Commons Attribution-NonCommercial 4.0 International License.