Android DownloadManager illegalstateexception unable to create directory

一笑奈何 提交于 2019-11-30 08:22:26

问题


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);

回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/17112142/android-downloadmanager-illegalstateexception-unable-to-create-directory

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!