I'm working on an app that can download files using DownloadManager. I set the download location to the external storage using this method:
setDestinationInExternalFilesDir(context, "temp_downloads", "filename");
The reason I decided to download to external storage is because some files are large (> 200 MB). The default download location which is a shared download cache directory has a limit of 200 MB size if I'm not mistaken and downloading more than 200 MB will cause the download to fail.
The problem is if a user that uses a device with sdcard ejects the sdcard while using the app, download will fail because setDestinationInExternalFilesDir method throws an IllegalStateException which will crash the app if not caught.
I have no issue with devices without sdcard slot because the system will generate an emulated external storage with path like: /storage/emulated/ and this storage will always be available. But if the device has sdcard slot, the external storage path will be /storage/sdcard0/ and if I try to call:
context.getExternalFilesDir(null);
while the sdcard is not mounted, it will return null.
Does this mean that DownloadManager will not work for downloading big files on device with ejected sdcard? or is there any work around for this problem?
I'm not looking for solutions like using a 3rd party library that downloads directly to internal storage or not using DownloadManager at all because using DownloadManager is a requirement.