Android DownloadManager illegalstateexception unable to create directory
Asked Answered
E

2

10

I'm making android app which is using DownloadManager. I want to download file into folder which I made. But this sources don't operate. And happen IllegalstateException. What can I do??

urlToDownload = Uri.parse(URL);
List<String> pathSegments = urlToDownload.getPathSegments();
request = new DownloadManager.Request(urlToDownload);
request.setTitle(Titlename);
request.setDescription("MCPE STORE");
request.setDestinationInExternalPublicDir(
                   Environment.getExternalStorageDirectory().getAbsolutePath() + 
                   "/MCPE STORE", pathSegments.get(pathSegments.size()-1));

Environment.getExternalStoragePublicDirectory(
                   Environment.getExternalStorageDirectory().getAbsolutePath() + 
                   "/MCPE STORE").mkdir();
latestId = downloadManager.enqueue(request);
Eldwun answered 14/6, 2013 at 15:34 Comment(0)
D
16

What can I do?

If you read the documentation for setDestinationInExternalPublicDir(), you will see that the first parameter is "the directory type to pass to getExternalStoragePublicDirectory(String)". That needs to be one of the constants defined on the Environment class, like Environment.DIRECTORY_DOWNLOADS. You are passing something else, which is not supported.

Diller answered 14/6, 2013 at 15:53 Comment(2)
What should I do if I want to have downloaded file in my app's special folder? Just download file in a constant folder defined by Environment class and then cut/paste file? any idea?Jactitation
@AliBehzadianNejad if I got you right,I did it like this: request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, File.separator + folderName + File.separator + filename);Somnus
D
6

Make sure you have

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

in your manifest.xml

Also if you are using an emulator, make sure you created it with SD card storage. It's NOT created by default.

Dongola answered 14/6, 2013 at 15:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.