问题
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