title: Android onDownloadStart get file name and extension from url
body: <p>I am trying to download files inside my Webview. I use <code>DownloladListener</code> to handle download request and then I use <code>DownloadManager</code> to download file. Everything works well but I can't get name and extension of file from url's like this one: <a href="http://unionpeer.com/dl.php?t=1502119" rel="nofollow noreferrer">http://unionpeer.com/dl.php?t=1502119</a> (This is assassins creed movie .torrent file). How can I get at least file extension? Thanks in advance!</p>
  
  <p>Downloading the file:</p>
  
  <pre><code>String url = (String) objects[0];
  
      String defaultDownloadPath = String.valueOf(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS));
  
      DownloadManager dm = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
      DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
  
      request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE)
              .setAllowedOverRoaming(true).setTitle(getFileName(url))
              .setDescription("Download")
              .setDestinationInExternalPublicDir(getPublicExternalPath(defaultDownloadPath),
                      getFileName(url));
      dm.enqueue(request);
  </code></pre>
  
  <p>getfileName() - works a little better than URLUtils.guessFileNameFromUrl()</p>
  
  <pre><code>String temp = "";
      String extension = url.substring(url.lastIndexOf("."));
      for (int i = 0; i &lt; extension.length(); i++) {
          if (i != 0){
              if (!Character.isLetter(extension.charAt(i)) &amp;&amp; !Character.isDigit(extension.charAt(i))) {
                   break;
              }
          }
          temp += extension.charAt(i);
      }
      return String.valueOf(System.currentTimeMillis() + temp);
  </code></pre>