Search code examples
androidandroid-download-manager

Android 10 / Q / API 29 - check if video file exists


I'm using this code to download videos:

val request = DownloadManager.Request(Uri.parse(fileURL))
.setDestinationInExternalPublicDir(
    Environment.DIRECTORY_PICTURES,
    "/appName/example.mp4"
)

but need to know if the file already exists.

I tried:

    val file = File(Environment.DIRECTORY_PICTURES + "/appName/example.mp4")
    
    if (file.isFile) {
        Log.d(tagg, "exists")
    } else {
        Log.d(tagg, "nope")
    }

and it says nope


Solution

  • val file = File(Environment.DIRECTORY_PICTURES + "/appName/example.mp4")

    That should be:

    val file = File(Environmet.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES )+ "/appName/example.mp4")
    

    On an Android 10 device your app has no access to that public diretory unless you requested legacy external storage in manifest file.

    You can check existence however without that permission.