Using DownloadManager to download to a new folder on the phone

Im using a download manager that downloads a file to a new folder on the phone. Here is the im code using:

DownloadManager.Request downloadSample = new DownloadManager.Request(Uri.parse(urlSample));
downloadSample.allowScanningByMediaScanner();
downloadSample.setDestinationInExternalPublicDir("/Samples/"+previewName, "sample.ttf");
downloadSample.setNotificationVisibility(DownloadManager.Request.VISIBILITY_HIDDEN);    

DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
manager.enqueue(downloadSample);

This works fine on many Ive devices, but several devices force the application to close with the following error in the log:

E/AndroidRuntime(29918): java.lang.IllegalStateException: Unable to create directory: /storage/sdcard0/Samples/Helvetica
E/AndroidRuntime(29918): at android.app.DownloadManager$Request.setDestinationInExternalPublicDir(DownloadManager.java:507)

It annoys him that it works great on some, but not at all on other devices. Does anyone know why this is happening?

+4
source share
1 answer

The documentation for setDestinationInExternalPublicDir (String dirType, String subPath) states that dirType can only accept the specified values.

dirType , Android, :

Environment.DIRECTORY_DOWNLOADS 

.

dirType : http://developer.android.com/reference/android/os/Environment.html#getExternalStoragePublicDirectory(java.lang.String)

, :

setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, subPath)

, "" . .

IllegalStateException .

0

Source: https://habr.com/ru/post/1538307/


All Articles