Search code examples
androidkotlinandroid-download-manager

Android 10 / API 29 Kotlin - download video to sub directory in pictures


I'm using this code to download videos:

val path = "Pictures/Appname"
val fileName = "example.mp4"
    
val request = DownloadManager.Request(Uri.parse(fileURL))
        .setDestinationInExternalPublicDir(path, fileName)

But it works only in API < 29

From API 29 I can only download it into Pictures directory by doing:

.setDestinationInExternalPublicDir(Environment.DIRECTORY_PICTURES, fileName)

even though the subfolder Appname exists in it.

What's the problem here?

I also tried those from the answers of other questions about this and none of them work:

.setDestinationInExternalPublicDir(path, fileName)
.setDestinationInExternalFilesDir(this@DownloadFile, path, fileName)
.setDestinationUri(Uri.fromFile(File(path, fileName)))

I would also accept a solutions without DownloadManager as long as it downloads the video to Pictures/Appname like I've seen other apps doing it also in API >= 29


Solution

  • The Downloadmanager does not need rights to store to external storage.

    The answer to create a subdir is in the comments.