title: Download file in Android
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/576513/android-download-binary-file-problems">Android download binary file problems</a>  </p>
  </blockquote>
  
  
  
  <p>I'm unsuccessfully trying to download a file from the internet. When the service starts, the size of downloaded file freezes at 325 B. I have got the internet connection and the permission for accessing internet and writing on external storage. </p>
  
  <pre><code>public class DownloadService extends IntentService {
  
      public DownloadService() {
          super(null);
      }
  
          @Override
      protected void onHandleIntent(Intent arg0) {
          Log.d("service", "onHandleIntent()");
          try {
              URL fileUrl = new URL("http://goo.gl/Mfyya");
  
                  HttpURLConnection urlConnection = (HttpURLConnection)fileUrl.openConnection();
                  urlConnection.setRequestMethod("GET");
              //  urlConnection.setDoOutput(true);
                  urlConnection.connect();
  
                  File sdcard = new File (Environment.getExternalStorageDirectory()+"/my");
                  File file = new File (sdcard, "filename.mp4");
  
                  FileOutputStream fileoutput = new FileOutputStream (file);
                  InputStream inputstream = urlConnection.getInputStream();
  
              //  int totalSize = urlConnection.getContentLength();
                  int downloadedSize = 0;
                  byte[] buffer = new byte[1024];
                  int bufferlength =0;
  
                  while ((bufferlength = inputstream.read(buffer)) &gt;= 0)
                  {   
                      fileoutput.write(buffer, 0, bufferlength);
                      downloadedSize += bufferlength;
                      //updateProgress(downloadedSize, totalSize);
                  }
                  fileoutput.close();
              } catch (IOException e) {
                  //Log.d("service", "error");
                  e.printStackTrace();
              }
      }
  }
  </code></pre>
  
  <p>What is going wrong?</p>