【问题标题】:Android: How to use download manager class?Android:如何使用下载管理器类?
【发布时间】:2011-08-18 04:15:48
【问题描述】:

我想从 url 下载二进制文件。是否可以使用我在这里找到的 Android 下载管理器类 DownloadManager class

【问题讨论】:

标签: java android url download


【解决方案1】:

确保 READ_EXTERNAL_STORAGE 和 WRITE_EXTERNAL_STORAGE 权限在您的 Manifest.xml 文件中:

然后将此代码包含在您的下载函数中

if(ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED
    || ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED){
    
    // this will request for permission when user has not granted permission for the app
    ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
}

else{
    //Download Script
    DownloadManager downloadManager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
    Uri uri = Uri.parse("URL of file to download");
    DownloadManager.Request request = new DownloadManager.Request(uri);
    request.setVisibleInDownloadsUi(true);
    request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
    request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, uri.getLastPathSegment());
    downloadManager.enqueue(request);
}

【讨论】:

    【解决方案2】:
    DownloadManager downloadmanager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
    Uri uri = Uri.parse("http://www.example.com/myfile.mp3");
    DownloadManager.Request request = new DownloadManager.Request(uri);
    request.setTitle("My File");
    request.setDescription("Downloading");
    request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
    request.setDestinationUri(Uri.parse("file://" + folderName + "/myfile.mp3"));
    downloadmanager.enqueue(request);
    

    【讨论】:

      【解决方案3】:

      使用 DownloadManager 类(仅限 GingerBread 和更新版本)

      GingerBread 带来了一个新功能,DownloadManager,它允许您轻松下载文件并将处理线程、流等的繁重工作委托给系统。

      首先,我们来看一个实用方法:

      /**
       * @param context used to check the device version and DownloadManager information
       * @return true if the download manager is available
       */
      public static boolean isDownloadManagerAvailable(Context context) {
      
          if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
              return true;
          }
          return false;
      }
      

      方法的名称说明了一切。一旦您确定 DownloadManager 可用,您可以执行以下操作:

      String url = "url you want to download";
      DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
      request.setDescription("Some descrition");
      request.setTitle("Some title");
      // in order for this if to run, you must use the android 3.2 to compile your app
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
          request.allowScanningByMediaScanner();
          request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
      }
      request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "name-of-the-file.ext");
      
      // get download service and enqueue file
      DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
      manager.enqueue(request);
      

      下载进度将显示在通知栏中。

      【讨论】:

        【解决方案4】:

        是否可以使用我在这里找到的 android 下载管理器类

        是的,不过这仅在 Android API 级别 9(2.3 版)之后才可用。 Here is a sample project 演示DownloadManager 的使用。

        【讨论】:

        • @CommonsWare ..我浏览了你的所有代码。一切都很棒。我想使用带wifi的DownloadManager ..我必须使用SFTP并从路由器下载文件..可以吗?请为此指导我..
        • @Satyam: DownloadManager 不支持 SFTP。 Android 中没有任何东西支持 SFTP AFAIK。您需要为此找到第三方 JAR。
        • 我发现我在 java 名称“JSCH”中使用的 API。 @CommonsWare 在我的 java 项目中它运行完美,但在 Android 中它不是......有可能吗?还是我可能面临其他问题?请回复..
        • @Satyam:如果您查看此页面的右上角,您应该会看到一个“”链接。那是用来提问的。 StackOverflow 中的评论可用于澄清现有答案,但不适用于诸如此类的不相关主题。
        • 对此我感到非常抱歉。我已经问过这个问题了:@ 987654322@ 匆忙忘记提交链接给你......它真的没有人回答,直到......同样......
        猜你喜欢
        • 2014-02-28
        • 2011-11-29
        • 2011-03-30
        • 2014-02-10
        • 2023-04-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多