title: How to write a large file from an applications assets folder to the SD Card without getting an Out of Memory Error?
body: <p>I am trying to copy a file of about 80 megabytes from the assets folder of an Android application to the SD card. </p>
  
  <p>The file is another apk. For various reasons I have to do it this way and can't simply link to an online apk or put it on the Android market. </p>
  
  <p>The application works fine with smaller apks but for this large one I am getting an out of memory error. </p>
  
  <p>I'm not sure exactly how this works but I am assuming that here I am trying to write the full 80 megabytes to memory.</p>
  
  <pre><code>try {
                  int length = 0;
                  newFile.createNewFile();
  
                  InputStream inputStream = ctx.getAssets().open(
                          "myBigFile.apk");
                  FileOutputStream fOutputStream = new FileOutputStream(
                          newFile);
                  byte[] buffer = new byte[inputStream.available()];
                  while ((length = inputStream.read(buffer)) &gt; 0) {
                      fOutputStream.write(buffer, 0, length);
                  }
                  fOutputStream.flush();
                  fOutputStream.close();
                  inputStream.close();
              } catch (Exception ex) {
                  if (ODP_App.getInstance().isInDebugMode())
                      Log.e(TAG, ex.toString());
              } 
  </code></pre>
  
  <p>I found this interesting - 
  <a href="https://stackoverflow.com/questions/477572/android-strange-out-of-memory-issue">A question about an out of memory issue with Bitmaps</a></p>
  
  <p>Unless I've misunderstood, in the case of Bitmaps, there appears to be some way to split the stream to reduce memory usage using BitmapFactory.Options.</p>
  
  <p>Is this do-able in my scenario or is there any other possible solution?  </p>