title: android application download file
body: <p>My current Android application downloads a number of audio files. When I employ this code to execute the download I get file not found exception:</p>
  
  <pre><code>try {
  
      final URL downloadFileUrl = new URL("http://filelocation/url.m4a");
      final HttpURLConnection httpURLConnection = (HttpURLConnection) downloadFileUrl.openConnection();
      httpURLConnection.setRequestMethod("GET");
      httpURLConnection.setDoOutput(true);
      httpURLConnection.setConnectTimeout(10000);
      httpURLConnection.setReadTimeout(10000);
      httpURLConnection.connect();
  
      mTrackDownloadFile = new File(Record.this.getCacheDir(), "mediafile");
      mTrackDownloadFile.createNewFile();
      final FileOutputStream fileOutputStream = new FileOutputStream(mTrackDownloadFile);
      final byte buffer[] = new byte[16 * 1024];
  
      final InputStream inputStream = httpURLConnection.getInputStream();
  
      int len1 = 0;
      while ((len1 = inputStream.read(buffer)) &gt; 0) {
          fileOutputStream.write(buffer, 0, len1);
      }
      fileOutputStream.flush();
      fileOutputStream.close();
  
  } catch (final Exception exception) {
      Log.i(TAG, "doInBackground - exception" + exception.getMessage());
      exception.printStackTrace();
      mTrackDownloadFile = null;
  }
  </code></pre>
  
  <p>When i employ this code it works fine:</p>
  
  <pre><code>try {
  
      final URL downloadFileUrl = new URL("http://filelocation/url.m4a");
      final URLConnection urlConnection = downloadFileUrl.openConnection();
  
      mTrackDownloadFile = new File(PlayOpponent.this.getCacheDir(), "mediafile");
      mTrackDownloadFile.createNewFile();
      final FileOutputStream fileOutputStream = new FileOutputStream(mTrackDownloadFile);
      final byte buffer[] = new byte[16 * 1024];
  
      final InputStream inputStream = urlConnection.getInputStream();
  
      int len1 = 0;
      while ((len1 = inputStream.read(buffer)) &gt; 0) {
          fileOutputStream.write(buffer, 0, len1);
      }
      fileOutputStream.flush();
      fileOutputStream.close();
  } catch (final Exception exception) {
      Log.i(TAG, "doInBackground - exception" + exception.getMessage());
      exception.printStackTrace();
      mTrackDownloadFile = null;
  }
  </code></pre>
  
  <p>Can someone please point out where I am going wrong?</p>