title: Android Appending 2 Mp4 Byte Arrays Together to Then Output 1 Mp4 File (where final video is first video plus second video)
body: <p>Hi I've been having a lot of trouble with this, I have 2 Files, both Mp4 format, read them into FileInputStreams, then into ByteArrayOutputStreams. I then try to append the two byte arrays by using another ByteArrayOutputStream [finalOutputStream] and write()'ing the two. Finally I use FileOutputStream to write(finalOutputStream.toByteArray()), flush, close. When i look for the video on my phone, there is a "Final" video that should have the 2 combined videos, with a size that looks like the two part's sizes added together.. but when i watch the video, it is only the second part -_- ... it is like the second part overwrites the first, but somehow the size increases?... heres some code.. </p>
  
  <pre><code>    File fileOne = new File(fileLongName);
      File fileTwo = new File(mediaStorageDir.getPath() + File.separator +"VID_TUTPART_"+ (foo-1) + ".mp4");
      FileInputStream fisOne = new FileInputStream(fileOne);
      FileInputStream fisTwo = new FileInputStream(fileTwo);
  
      int bufferSize = 1024;
      //FileOne
      byte[] buffer = new byte[bufferSize];
      ByteArrayOutputStream byteBuffer = new ByteArrayOutputStream();
  
      //FileTwo
      byte[] bufferTwo = new byte[bufferSize];
      ByteArrayOutputStream byteBufferTwo = new ByteArrayOutputStream();
  
      int len = 0;
      //FileOne to bytebuffer
      while ((len = fisOne.read(buffer)) != -1) {
        byteBuffer.write(buffer, 0, len);
      }
      //FileTwo to bytebuffer
      while ((len = fisTwo.read(bufferTwo)) != -1) {
        byteBufferTwo.write(buffer, 0, len);
      }
      byte[] byteArrayOne = byteBuffer.toByteArray();
      byte[] byteArrayTwo = byteBuffer.toByteArray();
  
      ByteArrayOutputStream finalOutputStream = new ByteArrayOutputStream( );
      finalOutputStream.write( byteArrayOne );
      finalOutputStream.write( byteArrayTwo );
  
      int counterFileNumber = 0;
      while(new File(mediaStorageDir.getPath() + File.separator +"VID_TO_TUTFIN_"+ counterFileNumber + ".mp4").exists()){
          counterFileNumber++;
      }
  
      String outputFileNameString = mediaStorageDir.getPath() + File.separator +"VID_TO_TUTFIN_"+ counterFileNumber + ".mp4";
      File outputFile = new File(outputFileNameString);
      FileOutputStream fos = new FileOutputStream(outputFile);
      fos.write(finalOutputStream.toByteArray());
      fos.flush();
      fos.close();
  </code></pre>