Android DownloadManager save to Download folder

﹥>﹥吖頭↗ 提交于 2019-12-10 23:51:16

问题


I'm using DownloadManager to download file, I wanted to put the downloads in the standard download folder so people could easily find them using file manager looking in the most obvious place:

Uri downloadLocation = Uri.fromFile(new File(Environment.DIRECTORY_DOWNLOADS, filename));
request.setDestinationUri(downloadLocation);
Long reference = downloadManager.enqueue(request);

But I get an exception because that isn't an external drive.

Is there no way to download a file to the user's download folder?

Also, what if they don't have external memory, just internal?

Thanks.


回答1:


Uri downloadLocation=Uri.fromFile(new File(Environment.DIRECTORY_DOWNLOADS, filename));

That is not how you use Environment.DIRECTORY_DOWNLOADS. That constant is for use with getExternalStoragePublicDirectory():

File dir=Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);

dir.mkdirs();

Uri downloadLocation=Uri.fromFile(new File(dir, filename);;

Note that the directory may not exist yet, so you should call mkdirs() yourself.

Also, what if they don't have external memory, just internal?

Practically all Android devices have external memory. External != removable. What the Android SDK refers to as "external storage" is what the user can access via the USB cable, and usually is on the same partition as "internal storage", in on-board flash memory.



来源:https://stackoverflow.com/questions/21921121/android-downloadmanager-save-to-download-folder

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