Get immediate access to this and over 1,500+ other videos and books.

Boost your skills with a raywenderlich.com Beginner subscription. With over 60+ video courses and our core foundational programming books bundled in one subscription, it’s simply the best investment you can make in your development career.

Use DownloadManager

The student materials have been reviewed and are updated as of SEPTEMBER 2022.

Replace the WorkManager usage when downloading images, with the DownloadManager, to display download progress.

Contributors

Heads up... You've reached locked video content where the transcript will be shown as obfuscated text.

You can unlock the rest of this video course, and our entire catalogue of books and videos, with a raywenderlich.com Professional subscription.

You’ve upgraded the starter project with a lot of fun features, using the WorkManager. One of the key features of the project is downloading images. and the simplest way to download an image is to use an HTTP connection manually.

val constraints = Constraints.Builder()
    .setRequiredNetworkType(NetworkType.NOT_ROAMING)
    .setRequiresBatteryNotLow(true)
    .setRequiresStorageNotLow(true)
    .build()

val localImageCheckWorker = OneTimeWorkRequestBuilder<LocalImageCheckWorker>()
    .setInputData(workDataOf("image_path" to imageUrl))
    .setConstraints(constraints)
    .build()

val workManager = WorkManager.getInstance(requireActivity())
workManager.enqueue(localImageCheckWorker)

workManager.getWorkInfoByIdLiveData(localImageCheckWorker.id)
val isDownloaded = workInfo.outputData.getBoolean("is_downloaded", false)

if (!isDownloaded) {
  val file = File(requireContext().externalMediaDirs.first(), imageUrl)
  
}
val request = DownloadManager.Request(Uri.parse("$BASE_URL/files/$imageUrl"))
    .setTitle("Image download")
    .setDescription("Downloading")
    .setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE)
    .setDestinationUri(Uri.fromFile(file))
    .setAllowedOverMetered(true)
    .setAllowedOverRoaming(false)
val downloadManager = requireContext().getSystemService(DownloadManager::class.java)
downloadManager?.enqueue(request)

Reviews

Comments