Android下载管理器

Android下载管理器,android,file,path,filepath,download-manager,Android,File,Path,Filepath,Download Manager,我想我有一个相当简单的问题 我一直在遵循上述url中的教程 如何更改下载的文件路径 提前感谢您可以使用此类信息配置对象。在本教程中,将在onClick()中创建并使用该Request对象 例如: DownloadManager.Request req=new DownloadManager.Request(uri); req.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI

我想我有一个相当简单的问题

我一直在遵循上述url中的教程

如何更改下载的文件路径

提前感谢

您可以使用此类信息配置对象。在本教程中,将在
onClick()
中创建并使用该
Request
对象

例如:

DownloadManager.Request req=new DownloadManager.Request(uri);

req.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI
                               | DownloadManager.Request.NETWORK_MOBILE)
   .setAllowedOverRoaming(false)
   .setTitle("Demo")
   .setDescription("Something useful. No, really.")
   .setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS,
                                      "test.mp4");

(以上代码来自)

Commonware答案的最后一行说明了目的地。他只使用SD卡上的常规下载文件夹,但您也可以这样做:

req.setDestinationInExternalPublicDir("/mnt/sdcard/Myfolder", "file_name.extension");

您确保用户允许您的应用访问外部存储。包含此代码以供下载

DownloadManager downloadManager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
     Uri uri = Uri.parse(url_string);
     DownloadManager.Request request = new DownloadManager.Request(uri);        
     request.setVisibleInDownloadsUi(true);        request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
    // include this line if permission has been granted by user to external directory
     request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, uri.getLastPathSegment());  
// or this line if not yet granted
request.setDestinationUri(Uri.parse("file://" + uri.getLastPathSegment());      
    Long reference = downloadManager.enqueue(request); 

您可以尝试执行以下操作:

  • 访问URL以了解下载管理器
  • 据我所知,您不能手动暂停/恢复下载
  • 如果取消下载,则完全取决于服务器是否支持暂停/恢复功能。如果是,则在取消下载后,如果您再次开始下载,它可能会恢复下载,而不会从头开始下载

  • 最后一行是很重要的:)起初我有点不明白。嗨,Commonware,我仍然不确定如何告诉DownloadManager将文件存储在哪个文件夹中:(.类似于异步OutputStream output=new FileOutputStream(“/sdcard/Myfolder/file_name.extension”);@Mich:
    setDestinationNext ternalPublicDir()
    和相关方法允许您将输出目录指定为
    文件
    ,而不是
    输出流
    。谢谢。刚刚开始了解it@CommonsWare如果我想将目的地更改为我的应用程序数据库文件夹,我该怎么办?我已尝试使用此代码
    .setDestinationUri(dst_uri);
    但它给出了由以下原因引起的错误:java.lang.IllegalArgumentException:不是文件URI:使用req.setDestinationUri(URI)或req.setDestinationNexternalPublicDir有什么坏处您也可以使用
    环境.getExternalStorageDirectory()
    而不是硬编码到“/mnt/sdcard”