title: Download pdf from Url and save it to sd card
body: <p>I have wrote this code for download <code>pdf</code> from <code>url</code> and file <code>url</code> is this-</p>
  
  <pre><code> String fileURL= "http://www.vivekananda.net/PDFBooks/History_of_India.pdf";
  </code></pre>
  
  <p><strong>Code this</strong></p>
  
  <pre><code>  public static void DownloadFile(String fileURL, File directory) {
  
      try {
  
          FileOutputStream f = new FileOutputStream(directory);
          URL u = new URL(fileURL);
          HttpURLConnection c = (HttpURLConnection) u.openConnection();
          c.setRequestMethod("GET");
          c.setDoOutput(true);
          c.getResponseCode();
          c.connect();
  
          InputStream in = c.getInputStream();
  
          byte[] buffer = new byte[1024];
          int len1 = 0;
          while ((len1 = in.read(buffer)) &gt; 0) {
              f.write(buffer, 0, len1);
          }
          f.close();
      } catch (Exception e) {
          e.printStackTrace();
      }
  
  }
  </code></pre>
  
  <p>but this shows file not found exception with response code 405.I dont know why this happened.Please help..!!</p>
  
  <p>This is my code where i had create file in sd card-</p>
  
  <p><strong>Code this</strong></p>
  
  <pre><code>  public void createPdfFile(){
          String extStorageDirectory = Environment.getExternalStorageDirectory()
                  .toString();
                  File folder = new File(extStorageDirectory, "pdf");
                  folder.mkdir();
                   file = new File(folder, "storrage_data.pdf");
                  try {
                      file.createNewFile();
                  } catch (IOException e1) {
                      e1.printStackTrace();
                  }        
      }`
  </code></pre>
  
  <p>After this i am calling download method in thread like this from onResume(); beacuse from onCreate it will give error "Network On Main Thread".where i am wrong now i don't konw :(</p>
  
  <p><strong>Code this</strong></p>
  
  <pre><code> public void downloadFile(){        
  
       new Thread(new Runnable() {
  
              @Override
              public void run() {             
           Downloader.DownloadFile(url, file);
            showPdf();
  
              }
          }).start();   
  
  
      }
  </code></pre>