Android downloadmanager下载位置

时间:2017-05-08 01:12:59

标签: java android download uri android-download-manager

我尝试使用/storage/emulated/0/Download

将文件从网址下载到DownloadManager
DownloadManager downloadManager = (DownloadManager) context.getApplicationContext().getSystemService(DOWNLOAD_SERVICE);
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
request.allowScanningByMediaScanner();
request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE);
request.setAllowedOverRoaming(false);
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setTitle("blah");
String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getAbsoluteFile().toString();
System.out.println(path);
request.setDestinationInExternalPublicDir(path, title);
request.setVisibleInDownloadsUi(true);

final long downloadReference = downloadManager.enqueue(request);

我确认path确实返回了所需的下载路径。我的问题是,当文件实际下载时,它们会以/documents/blah结束,如果卸载了应用程序,则会从设备中删除文件。

1 个答案:

答案 0 :(得分:0)

你试过吗

request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, file.name());