kyazzeallan4
05/30/2024, 10:53 AMfun downloadVideo(videoUrl: String, context: Context, downloadManager: DownloadManager, videoTitle: String) { val videoUri = videoUrl.toUri()
val moviesDir = File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES), "Movies") moviesDir.mkdirs()
val videoFile = File(moviesDir, videoTitle)
val request = DownloadManager.Request(videoUri) .setTitle(videoTitle)
.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED) .setAllowedOverMetered(true)
.setAllowedOverRoaming(true) .setAllowedNetworkTypes(DownloadManager.Request.NETWORK_MOBILE or DownloadManager.Request.NETWORK_WIFI)
.setMimeType("video/mp4") .addRequestHeader("Authorization", "Bearer <token>")
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { // Use setDestinationUri for Android 10 and above
request.setDestinationUri(videoFile.toUri()) } else {
// Use setDestinationInExternalPublicDir for Android 9 and below request.setDestinationInExternalPublicDir(Environment.DIRECTORY_MOVIES, "Movies/$videoTitle")
}
downloadManager.enqueue(request)}