2017-05-08 117 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());