Skip to content

Instantly share code, notes, and snippets.

@ziginsider
Created October 13, 2021 13:31
Show Gist options
  • Save ziginsider/63452b11716c8ca5fd3c81ed5b001afc to your computer and use it in GitHub Desktop.
Save ziginsider/63452b11716c8ca5fd3c81ed5b001afc to your computer and use it in GitHub Desktop.
Download to device by url
class MyDownloadManager {
fun downloadByUrl(context: Context, url: String) {
val fileName = url.substring(url.lastIndexOf('/') + 1)
val request = DownloadManager.Request(Uri.parse(url)).apply {
setTitle("image $fileName")
setDescription("From CatGallery")
setNotificationVisibility(VISIBILITY_VISIBLE_NOTIFY_COMPLETED)
setDestinationInExternalPublicDir(DIRECTORY_DOWNLOADS, fileName)
}
(context.getSystemService(DOWNLOAD_SERVICE) as DownloadManager).enqueue(request)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment