title: Download and View Pdf in Internal Storage using Async Task in android
body: <p>I am trying to programmatically download and read pdf from a server using AsyncTask but I get no response. I also have a <code>seekBar</code> which would show download progress. </p>
  
  <p>Where am i getting it wrong?</p>
  
  <pre><code>private void initSeekBar() {
          seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
              @Override
              public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                 int val = (progress * (seekBar.getWidth() - 3
                 * seekBar.getThumbOffset())) / seekBar.getMax();
                 mProgressText.setText("" + progress);
                 mProgressText.setX(seekBar.getX() + val + seekBar.getThumbOffset() / 2);
              }
  
              @Override
              public void onStartTrackingTouch(SeekBar seekBar) {
  
              }
  
              @Override
              public void onStopTrackingTouch(SeekBar seekBar) {
  
              }
          });
      }
  </code></pre>
  
  <p>to download file </p>
  
  <pre><code>private void downloadFile(final String fileName){
      new AsyncTask&lt;Void, Integer, Boolean&gt;() {
          @Override
          protected Boolean doInBackground(Void... params) {
              return downloadFile();
          }
          @Nullable
          private Boolean downloadFile(){
              try {
                  File file = getFileStreamPath(fileName);
                  if (file.exists()) {
                      downloadButton.setText("Start Reading");
                      return true;
                  }
                  try {
                      seekBar.setVisibility(View.VISIBLE);
                      mProgressText.setVisibility(View.VISIBLE);
                      FileOutputStream fileOutputStream = openFileOutput(fileName, Context.MODE_PRIVATE);
                      URL url = new URL(document);
                      URLConnection urlConnection = url.openConnection();
                      int contentLength = urlConnection.getContentLength();
                      InputStream inputStream = new BufferedInputStream(url.openStream());
                      byte data[] = new byte[contentLength];
                      long total = 0;
                      int count;
                      while ((count = inputStream.read(data)) != -1) {
                          total += count;
                          publishProgress((int) ((total * 100) / contentLength));
                          fileOutputStream.write(data, 0, count);
  
                      }
                      fileOutputStream.flush();
                      fileOutputStream.close();
                      inputStream.close();
                      return true;
                  } catch (final Exception e) {
                      e.printStackTrace();
                      return false; //swallow a 404
                  }
              }catch (Exception e){
                    e.printStackTrace();
                  }
                  return false;
  
              }
  
          @Override
          protected void onProgressUpdate(Integer... values) {
              super.onProgressUpdate(values);
              seekBar.setProgress(values[0]);
          }
  
          @Override
          protected void onPostExecute(Boolean aBoolean) {
              super.onPostExecute(aBoolean);
              if(aBoolean){
                  downloadButton.setText("Start Reading");
              } else {
                  Toast.makeText(BookDetailsActivity.this, "Download error", Toast.LENGTH_SHORT).show();
                  downloadButton.setText("Download");
              }
              seekBar.setVisibility(View.GONE);
              mProgressText.setVisibility(View.GONE);
          }
      };
  
  }
  </code></pre>