Search code examples
androidpathandroid-download-manager

DownloadManager directly into external storage directory


I'm trying to download a big zip file directly inside the external storage directory, for example

"/mnt/sdcard/Android/data/com.vexdev.audioguida.app/files/data"

i guess i should be using

DownloadManager.Request.setDestinationInExternalPublicDir(path, fileName)

but i don't know how to provide it with a path that is consistent across different android devices.

AND i'm also trying to get a path to access this file later, like this:

File file = new File(path + fileName);

i'm asking here because i know that those two methods are expecting different rooted paths, because i tried to provide a path like this:

Application.getAppContext().getExternalFilesDir(Environment.getDataDirectory().getAbsolutePath()).getAbsolutePath();

but the DownloadManager was not putting the files where the File constructor was searching it later. (It was downloading in the wrong directory actually!)

I'm looking for a way to download those files directly in the right directory, without having to move them.


Solution

  • i guess i should be using DownloadManager.Request.setDestinationInExternalPublicDir(path, fileName)

    Not for the location you suggested. The closer match would be setDestinationInExternalFilesDir().

    but i don't know how to provide it with a path that is consistent across different android devices.

    There isn't even a path that will be consistent within one device, as different accounts will use different paths. For example, the path you typed into your question might be used on some devices for some accounts, but the details will vary.

    i'm also trying to get a path to access this file later

    That is covered in the DownloadManager.Request documentation:

    • setDestinationInExternalFilesDir() maps to getExternalFilesDir() on Context

    • setDestinationInExternalPublicDir() maps to getExternalStoragePublicDirectory() on Environment

    i tried to provide a path like this

    That is not how you use getExternalFilesDir(). Please read the JavaDocs to see what valid values are for the parameter to that method. getDataDirectory() is not a valid value.