繁体   English   中英

如何在从 Url 下载文件时设置我选择的名称

[英]how to set name of my choice in downloading file from Url

我想在从 Url 下载文件时设置我选择的名称。

    DownloadManager.Request request = new DownloadManager.Request(Uri.parse(URL));
    if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.JELLY_BEAN){
        request.allowScanningByMediaScanner();
        request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
        DownloadManager downloadManager = (DownloadManager)getSystemService(Context.DOWNLOAD_SERVICE);
        request.allowScanningByMediaScanner();
        request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_MOBILE | DownloadManager.Request.NETWORK_WIFI);
        downloadManager.enqueue(request);

您可以在 subPath 中设置您的文件名

  val request = DownloadManager.Request(Uri.parse(url))
    request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI or 
    DownloadManager.Request.NETWORK_MOBILE)
    request.setTitle("Download")
    request.setDescription("The File is Downloading..")
    request.allowScanningByMediaScanner()
    request.setMimeType("application/pdf")
    request.setNotificationVisibility
   (DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED)
    request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS,
        "${System.currentTimeMillis()}" + ".pdf")

    // get download service and  enqueue file
    val manager = getSystemService(Context.DOWNLOAD_SERVICE) as DownloadManager
    manager.enqueue(request)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM