2017-05-08 97 views
1

我试图从URL下载文件到/storage/emulated/0/Download使用DownloadManagerAndroid的下载管理器下载位置

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,如果应用程序已卸载,则会从设备中删除文件。

回答

0

你试过

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