0

I need to download a file to the getFilesDir directory of android so it is only accessible by the application. I am currently using Download Manager and I can only specify the Environment type. Please can I use a custom directory or is there another approach.

This is my download code:

public void download(){
    DownloadManager.Request request = new DownloadManager.Request(Uri.parse(videoUrl));
    request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI| DownloadManager.Request.NETWORK_MOBILE);
    request.setTitle("Download");
    request.setDestinationInExternalFilesDir(getApplicationContext(),Environment.DIRECTORY_DOWNLOADS, File.separator + "lessons/1234.mp4");
    manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
    manager.enqueue(request);
    registerReceiver(downloadComplete, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
}
Joseph Ofem
  • 304
  • 2
  • 15
  • Use an HTTP client API and download it yourself. I use [OkHttp for downloading files](https://stackoverflow.com/a/29012988/115145). – CommonsWare Mar 31 '21 at 10:36
  • Check comment here https://stackoverflow.com/questions/39744505/how-to-save-files-with-downloadmanager-in-private-path#comment66786872_39744924 – Zain Mar 31 '21 at 21:52

1 Answers1

0

Define relativePath=Environment.getExternalStorageDirectory().getPath() + "MyExternalStorageAppPath"

And then use setDestinationInExternalFilesDir(context, relativePath, filename);

Also don't forget to add android:requestLegacyExternalStorage="true" in your manifest.

So by using "setDestinationInExternalFilesDir" files will be download to the external memory dedicated to your app, so: "external_storage/Android/data/name_of_your_app/path_passed

Hack Try
  • 471
  • 4
  • 17