title: Save a pdf file in local memory in Android
body: <p>I'm trying to save a pdf file from a URL in my Android Project, but when it saves the .pdf file, it has a dimension of 0KB. The code creates the directory and the PDF file (Ciao.pdf), I also have INTERNET and WRITE_EXTERNAL_STORAGE permission in my AndroidManifest.xml. 
  This is the code: </p>
  
  <pre><code>        LinkTo = "http://www.ancestralauthor.com/download/sample.pdf";
          Toast.makeText(getApplicationContext(),LinkTo, Toast.LENGTH_LONG).show();
          String storage = Environment.getExternalStorageState();
          Log.i("System out", "" + storage);
          File rootsd = Environment.getExternalStorageDirectory();
          Log.d("ROOT PATH", "" + rootsd.getAbsolutePath());
          File mydir = new File(rootsd.toString() + "/unplugger");
          Log.i("DIR PATH", "" + mydir.getAbsolutePath());
          mydir.mkdir();
          URL u;
          try {
              u = new URL(LinkTo);
  
              HttpURLConnection c = (HttpURLConnection) u.openConnection();
              c.setRequestMethod("GET");
              c.setDoOutput(true);
              c.connect();
              FileOutputStream f = new FileOutputStream(new File(mydir + "/"
                      + "Ciao.pdf"));
  
              InputStream input = c.getInputStream();
  
              byte[] buffer = new byte[1024];
              int len1 = 0;
              while ((len1 = input.read(buffer)) &gt; 0) {
                  f.write(buffer, 0, len1);
                  f.flush();
              }
              f.close();
              c.disconnect();
          } catch (MalformedURLException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
              Log.i("System out","malformed :"+e.getMessage());
              Log.i("System out","malformed :"+e.toString());
          } catch (IOException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
              Log.i("System out","io: "+e.getMessage());
              Log.i("System out","io: "+e.toString());
          }
  </code></pre>