【问题标题】:Android DownloadManager get filenameAndroid DownloadManager 获取文件名
【发布时间】:2012-10-30 14:15:26
【问题描述】:

在我的应用程序中,您可以下载一些文件。我使用 Android DownloadManager 类进行下载。下载完成后,它应该向我显示文件已下载的消息。问题是,可能同时有 2,3 或 4 个下载。我的BroadcastReceiver 代码如下所示:

receiver_complete = new BroadcastReceiver(){
         @Override
          public void onReceive(Context context, Intent intent) {
             String action = intent.getAction();
                if (action.equals(DownloadManager.ACTION_DOWNLOAD_COMPLETE) ){
                    Toast.makeText(MainActivity.this, MainActivity.this.getString(R.string.download_finished, "Here should be the name", Toast.LENGTH_SHORT).show();
                }
         }
     };

如何获取已完成下载的当前文件名?

非常感谢。

【问题讨论】:

    标签: java android broadcastreceiver android-download-manager receiver


    【解决方案1】:

    我想你想把这样的东西放在你的if 块中。将 YOUR_DM 替换为您的 DownloadManager 实例。

    Bundle extras = intent.getExtras();
    DownloadManager.Query q = new DownloadManager.Query();
    q.setFilterById(extras.getLong(DownloadManager.EXTRA_DOWNLOAD_ID));
    Cursor c = YOUR_DM.query(q);
    
    if (c.moveToFirst()) {
        int status = c.getInt(c.getColumnIndex(DownloadManager.COLUMN_STATUS));
        if (status == DownloadManager.STATUS_SUCCESSFUL) {
            // process download
            title = c.getString(c.getColumnIndex(DownloadManager.COLUMN_TITLE));
            // get other required data by changing the constant passed to getColumnIndex
        }
    }
    

    【讨论】:

    • 谢谢!完美运行!
    • 如何在 BroadcastReceiver 中使用此代码?
    【解决方案2】:

    Ian Shannon 的回答完全正确,但我建议进行一些改进:

    • 记得在使用后关闭Cursor,避免“光标泄漏”。这个Cursor会消耗大量资源,必须尽快释放。

    • 如果你给下载放了一些标题,比如:

      DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
      request.setTitle("Some title");
      

      DownloadManager.COLUMN_TITLE 给出的值将是"Some title",而不是文件名。所以我会推荐这个:

      String filePath = c.getString(c.getColumnIndex(DownloadManager.COLUMN_LOCAL_FILENAME));
      title = filePath.substring( filePath.lastIndexOf('/')+1, filePath.length() );
      

      COLUMN_LOCAL_FILENAME 返回整个路径 (/storage/sdcard0/Android/data/.../filename.ext),但使用此代码,我们只会获取文件名。

    最终代码:

    Bundle extras = intent.getExtras();
    DownloadManager.Query q = new DownloadManager.Query();
    q.setFilterById(extras.getLong(DownloadManager.EXTRA_DOWNLOAD_ID));
    Cursor c = YOUR_DM.query(q);
    
    if (c.moveToFirst()) {
        int status = c.getInt(c.getColumnIndex(DownloadManager.COLUMN_STATUS));
        if (status == DownloadManager.STATUS_SUCCESSFUL) {
            String filePath = c.getString(c.getColumnIndex(DownloadManager.COLUMN_LOCAL_FILENAME));
            filename = filePath.substring( filePath.lastIndexOf('/')+1, filePath.length() );
        }
    }
    c.close();
    

    编辑:将 YOUR_DM 替换为您的 DownloadManager 实例。

    【讨论】:

    • 请注意COLUMN_LOCAL_FILENAME弃用。您可以使用COLUMN_LOCAL_URI 获取Uri,这是实际文件的Uri。
    【解决方案3】:

    我认为您正在寻找的是DownloadManager.COLUMN_TITLE

    这里是 Android 文档的链接:http://developer.android.com/reference/android/app/DownloadManager.html#COLUMN_TITLE

    这里有一个教程,它解释的不仅仅是获得标题。它用于从 URL 下载图像,然后将其显示在应用程序中。总的来说,我认为非常有用。

    http://wptrafficanalyzer.in/blog/downloading-an-image-from-an-http-url-using-downloadmanager-and-displaying-in-imageview-by-dynamically-registered-broadcastreceiver/

    【讨论】:

      【解决方案4】:

      有一种更简单的方法来检索下载的文件 URI,那就是getUriForDownloadedFile

      download_receiver = new BroadcastReceiver() {
          @Override
          public void onReceive(Context context, Intent intent) {
              Bundle b = intent.getExtras();
              DownloadManager dm = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
              long file_id = b.getLong(DownloadManager.EXTRA_DOWNLOAD_ID);
              Uri uri = dm.getUriForDownloadedFile(downloaded_file_id);
      
          }
      }
      

      我在关于如何使用 BroadcastReceiver 处理 DOWNLOAD_COMPLETE 事件的完整描述中找到了这个方法 here

      【讨论】:

        【解决方案5】:

        你试试这个代码来获取文件名

        下载管理器.COLUMN_LOCAL_FILENAME

        我不确定

        【讨论】:

          猜你喜欢
          • 2016-02-26
          • 2014-05-28
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-03-12
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多