title: how to download pdf file from server , store it in sdcard and view through intent in android?
body: <p>I have written a program to download my pdf file from server into my sdcard , which is getting downloaded properly but when i am viewing it through my pdfviewer it says unable to open this file.
  Also the files size is 4.5mb , but the pdf which is getting downloaded into my sdcards pdf folder has size 0 .</p>
  
  <pre><code>private void processPDF(String subject_pdf) {
      new AsyncTask&lt;Void, Void, String&gt;() {
          @Override
          protected String doInBackground(Void... params) {
              String result = "";
              StringBuilder builder = new StringBuilder(100000);
              try {
                  String URL = 
                  "http://192.168.1.3:8082/APP_Server/subject_pdfs_result.jsp? 
                   branch_year="+ type; //type = branches(e.g computers , IT) 
                  HttpClient httpclient = new DefaultHttpClient();
                  HttpGet httpget = new HttpGet(URL);
                  HttpResponse response = httpclient.execute(httpget);
                  InputStream is = response.getEntity().getContent();
                  BufferedReader buffer = new BufferedReader(new 
                  InputStreamReader(is));
                  // result = is.toString();
                  while ((result = buffer.readLine()) != null) {
                      builder.append(result);
                  }
  
              } catch (Exception ex) {
                  ex.printStackTrace();
  
              }
              return builder.toString().replaceAll("\\&lt;.*?\\&gt;", "");
          }
  
          @Override
          protected void onPostExecute(String builder) {
              //url has the pdf name 
              name= builder.replace("\t", "");
              image_url = 
              "http://192.168.1.3:8082/APP_Server/Images/Books_Pdf/" + url;
              extStorageDirectory = 
              Environment.getExternalStorageDirectory().toString();
              File folder = new File(extStorageDirectory, "pdf");
              folder.mkdir();
              File file = new File(folder, name);
              try {
                  file.createNewFile();
              } catch (Exception e1) {
                  e1.printStackTrace();
              }
              PDFDownloader.DownloadFile(image_url, file);
  
              showPdf();
  
          }
  
      }.execute(null, null, null);
  
  }
  
  private void showPdf() {
      File file = new File(Environment.getExternalStorageDirectory() + "/pdf/" 
                 + name);
      PackageManager packageManager = getPackageManager();
      Intent testIntent = new Intent(Intent.ACTION_VIEW);
      testIntent.setType("application/pdf");
      List list = packageManager.queryIntentActivities(testIntent, 
      PackageManager.MATCH_DEFAULT_ONLY);
      Intent intent = new Intent();
      intent.setAction(Intent.ACTION_VIEW);
      Uri uri = Uri.fromFile(file);
      intent.setDataAndType(uri, "application/pdf");
      startActivity(Intent.createChooser(intent, "Open File"));
  
  }
  </code></pre>
  
  <p>PDFDownloader.java</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.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>Please need some guidance as to where i am going wrong.
  Thank You.</p>