【问题标题】:Android Download Manager安卓下载管理器
【发布时间】:2012-06-06 00:16:03
【问题描述】:

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

http://www.vogella.com/blog/2011/06/14/android-downloadmanager-example/

我一直在关注上述网址中的教程。

如何更改下载的文件路径?

提前致谢

【问题讨论】:

    标签: android file path filepath download-manager


    【解决方案1】:

    您使用此类信息配置DownloadManager.Request 对象。在本教程中,Request 对象在onClick() 中创建和使用。

    例如:

    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");
    

    (以上代码来自this sample project)

    【讨论】:

    • 最后一行是重要的:) 起初我偶然发现了一点。
    • 嗨 CommonsWare 我仍然不确定如何告诉 DownloadManager 将文件存储在哪个文件夹中:(。就像你在 Async OutputStream output = new FileOutputStream("/sdcard/Myfolder/file_name .extension");
    • @Mich: setDestinationInExternalPublicDir() 和相关方法让您可以将输出目录指定为File,而不是OutputStream。
    • 谢谢。刚刚开始了解它
    • @CommonsWare 如果我想将目的地更改为我的应用程序数据库文件夹。我该怎么办?我已经尝试使用此代码.setDestinationUri(dst_uri);,但它给出了错误 Caused by: java.lang.IllegalArgumentException: Not a file URI:
    【解决方案2】:

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

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

    【讨论】:

    • 使用 req.setDestinationUri(uri) 或 req.setDestinationInExternalPublicDir 的缺点是什么
    • 你也可以使用Environment.getExternalStorageDirectory()而不是硬编码到"/mnt/sdcard"
    【解决方案3】:

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

    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); 
    

    【讨论】:

      【解决方案4】:

      您可以尝试以下操作:

      1. 访问网址了解下载管理器https://github.com/quoraboy/DownloadManagerInAndroid
      2. 据我了解,您无法手动暂停/恢复下载。
      3. 如果您取消下载,则完全取决于您的服务器是否支持暂停/恢复功能。如果是,那么在取消下载后,如果您再次开始下载,它实际上可能会恢复下载,并且不会从头开始下载。

      【讨论】:

      • 可以在pause和resumedownload manager下载
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-10-05
      • 2014-02-23
      • 2012-10-31
      • 1970-01-01
      • 2010-12-19
      • 2012-02-29
      • 2016-05-25
      相关资源
      最近更新 更多