Android DownloadManager get filename

前端 未结 5 730
醉酒成梦
醉酒成梦 2020-12-08 10:37

In my app you can download some files. I used the Android DownloadManager class for downloading. After the download is completed, it should show me a message th

相关标签:
5条回答
  • 2020-12-08 10:54

    I think you want to put something like this inside your if block. Replace YOUR_DM with your DownloadManager instance.

    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
        }
    }
    
    0 讨论(0)
  • 2020-12-08 10:54

    There's a simpler way to retrieve the downloaded file URI, and that is with 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);
    
        }
    }
    

    I found this method here in a full description of how to use a BroadcastReceiver for handling DOWNLOAD_COMPLETE events.

    0 讨论(0)
  • 2020-12-08 10:55

    I think what you are looking for is the DownloadManager.COLUMN_TITLE

    Here is a link to the Android Documents: http://developer.android.com/reference/android/app/DownloadManager.html#COLUMN_TITLE

    And here is a tutorial that will explain more than just getting the title. It is for downloading an image from a URL and then displaying it in an application. Mighty useful I think, overall speaking.

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

    0 讨论(0)
  • 2020-12-08 11:09

    u try this code to get the file name

    DownloadManager.COLUMN_LOCAL_FILENAME

    i am not sure about it

    0 讨论(0)
  • 2020-12-08 11:17

    Ian Shannon was totally right with his answer, but I sugest some improvement:

    • Remember to close that Cursor after using it, avoiding "Cursor Leaking". This Cursor consumes a lot of resources and must be released as soon as possible.

    • If you put some title for the download, such as:

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

      The value given by the DownloadManager.COLUMN_TITLE will be "Some title" instead of the file name. So I would recommend this instead:

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

      The COLUMN_LOCAL_FILENAME returns the entire path (/storage/sdcard0/Android/data/.../filename.ext), but with this code, we will only get the file name.

    Final code:

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

    Edit: Replace YOUR_DM with your DownloadManager instance.

    0 讨论(0)
提交回复
热议问题