Skip to main content

How to query and retrieve downloaded files using DownloadManager in Kotlin Android

How to query and retrieve downloaded files using DownloadManager in Kotlin Android.

Here's a step-by-step tutorial on how to query and retrieve downloaded files using DownloadManager in Kotlin Android:

Step 1: Add the necessary permissions to your AndroidManifest.xml file:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

Step 2: Create a new Kotlin class, let's call it DownloadHelper, to handle the download process:

import android.app.DownloadManager
import android.content.Context
import android.net.Uri
import android.os.Environment

class DownloadHelper(private val context: Context) {

fun downloadFile(url: String, fileName: String) {
val request = DownloadManager.Request(Uri.parse(url))
request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI or DownloadManager.Request.NETWORK_MOBILE)
request.setTitle(fileName)
request.setDescription("Downloading file...")
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED)
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, fileName)

val downloadManager = context.getSystemService(Context.DOWNLOAD_SERVICE) as DownloadManager
downloadManager.enqueue(request)
}

fun getDownloadedFiles(): List<DownloadedFile> {
val downloadManager = context.getSystemService(Context.DOWNLOAD_SERVICE) as DownloadManager
val query = DownloadManager.Query()
query.setFilterByStatus(DownloadManager.STATUS_SUCCESSFUL)

val cursor = downloadManager.query(query)
val downloadedFiles = mutableListOf<DownloadedFile>()

if (cursor.moveToFirst()) {
do {
val id = cursor.getLong(cursor.getColumnIndex(DownloadManager.COLUMN_ID))
val title = cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_TITLE))
val uri = cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI))

downloadedFiles.add(DownloadedFile(id, title, uri))
} while (cursor.moveToNext())
}

cursor.close()
return downloadedFiles
}

data class DownloadedFile(val id: Long, val title: String, val uri: String)
}

Step 3: In your activity or fragment, create an instance of DownloadHelper and use its methods to download files and retrieve downloaded files:

val downloadHelper = DownloadHelper(context)

// Download a file
downloadHelper.downloadFile("https://example.com/file.pdf", "file.pdf")

// Retrieve downloaded files
val downloadedFiles = downloadHelper.getDownloadedFiles()
for (file in downloadedFiles) {
Log.d("DownloadedFile", "ID: ${file.id}, Title: ${file.title}, URI: ${file.uri}")
}

That's it! You now have the ability to download files using DownloadManager and retrieve the downloaded files using DownloadHelper in Kotlin Android.

Note: Don't forget to handle the necessary runtime permissions for downloading and saving files in Android 6.0 (Marshmallow) and above.

I hope this tutorial helps you in querying and retrieving downloaded files using DownloadManager in Kotlin Android! If you have any further questions, feel free to ask.