title: How to download a file after clicking a button (Android Studio)
body: <p>I recently created an activity in my app. Now I wanted the user to download a .pdf file when he/she wants to view the guidelines. I wanted to implement this on a button. Any idea how to do this properly?</p>
  
  <p>Heres my code below:</p>
  
  <pre><code>public class Exhibitor_Registration_Activity extends AppCompatActivity  {
  
      Button buttonDownload;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_exhibitor_registration_);
  
          this.setTitle("Buyer Registration");
  
          Toolbar myToolbar = (Toolbar) findViewById(R.id.my_toolbar);
          setSupportActionBar(myToolbar);
          getSupportActionBar().setDisplayHomeAsUpEnabled(true);
          myToolbar.setNavigationIcon(R.drawable.ic_arrow_back_white_24dp);
  
          final Button buttonDownload = (Button) findViewById(R.id.buttonDownload);
  
          buttonDownload.setOnClickListener(new View.OnClickListener(){
  
              @Override
              public void onClick(View view) {
                  try {
                      //this is the file you want to download from the remote server
                      String path ="http://www.manilafame.com/website-assets/downloads/exhibitor-application-kit/local/201704/1-Summary-of-Participation-Details-April-2017_MN_002.pdfp";
                      //this is the name of the local file you will create
                      String targetFileName = null;
                      boolean eof = false;
                      URL u = new URL(path);
                      HttpURLConnection c = (HttpURLConnection) u.openConnection();
                      c.setRequestMethod("GET");
                      c.setDoOutput(true);
                      c.connect();
                      FileOutputStream f = new FileOutputStream(new File("c:\\junk\\"+targetFileName));
                      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 (MalformedURLException e) {
                      // TODO Auto-generated catch block
                      e.printStackTrace();
                  } catch (ProtocolException e) {
                      // TODO Auto-generated catch block
                      e.printStackTrace();
                  } catch (FileNotFoundException e) {
                      // TODO Auto-generated catch block
                      e.printStackTrace();
                  } catch (IOException e) {
                      // TODO Auto-generated catch block
                      e.printStackTrace();
                  }
              }
          });
      }
  }
  </code></pre>
  
  <p>I also got the source code from <a href="https://stackoverflow.com/questions/576513/android-download-binary-file-problems">here</a> and <a href="https://stackoverflow.com/questions/6368788/how-to-download-a-pdf-file-in-android">here</a>.</p>