Drive KitでHUAWEI Driveからファイルをダウンロードする方法


HUAWEI Driveからのダウンロード手順

  1. HUAWEI Driveに接続する
  2. ファイルオブジェクトを取得する
  3. HUAWEI Driveのダウンロードリクエストを実行する

注意点
1. ファイルサイズが20MB以下の場合、isDirectDownloadEnabledをtrueに設定できます。

サンプル

private var drive: Drive? = null

fun load(filename: String, dest: File, isApplicationData: Boolean) {
    getFile(filename, false, isApplicationData)?.let { file ->
        download(file, dest)
    }
}

private fun download(file: com.huawei.cloud.services.drive.model.File, dest: File) {
    drive?.let { drive ->
        val size: Long = file.getSize()
        val isDirectDownload = file.getSize() < DIRECT_DOWNLOAD_MAX_SIZE
        drive.files()[file.id].let { get ->
            get.mediaHttpDownloader.setContentRange(0, size - 1).isDirectDownloadEnabled = isDirectDownload
            get.executeContentAndDownloadTo(FileOutputStream(dest))
        }
    }
}

GitHub

HMS Drive Kit Demo : https://github.com/Rei2020GitHub/MyPublicProject/tree/master/DriveKitDemo

参考