簡體   English   中英

下載管理器-設置文件擴展名而無需硬編碼

[英]Download Manager - setting file extension without hardcoding

我想使用不包含文件類型的網址來下載任何擴展名的文件。 在這里無法對文件類型進行硬編碼。

Uri downloadUrl = Uri.parse(link); 
DownloadManager.Request request = new DownloadManager.Request(downloadUrl);
request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE);
request.setAllowedOverRoaming(true);
request.setTitle(title);
request.setDescription(description); 
request.setVisibleInDownloadsUi(true);
request.setMimeType("application/pdf"); 
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, subDirectory);
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
DownloadManager downloadManager = (DownloadManager) context.getSystemService(DOWNLOAD_SERVICE);
if (downloadManager != null) {
    downloadManager.enqueue(request);
}

嘗試這個:

File file=new File(getExternalFilesDir(null),"Dummy");
/*
Create a DownloadManager.Request with all the information necessary to start the download
*/
DownloadManager.Request request = 
    new DownloadManager.Request(Uri.parse("YOUR URL"))
       .setTitle("Dummy File")// Title of the Download Notification
       .setDescription("Downloading")// Description of the Download Notification
       .setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE)// Visibility of the download Notification
       .setDestinationUri(Uri.fromFile(file))// Uri of the destination file
       .setRequiresCharging(false)// Set if charging is required to begin the download
       .setAllowedOverMetered(true)// Set if download is allowed on Mobile network
       .setAllowedOverRoaming(true);// Set if download is allowed on roaming network

然后:

DownloadManager downloadManager= 
    (DownloadManager) getSystemService(DOWNLOAD_SERVICE);

// enqueue puts the download request in the queue.
downloadID = downloadManager.enqueue(request);

有關完整示例,請參見鏈接

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM