title: base64 encode audio file and send as a String then decode the String
body: <p>I have been stuck on this issue for a few hours now trying to get it working. Basically what I am trying to do is the following. Base64 encode an audio file picked up from an sdcard on an Android device, Base64 encode it, convert it into a String and then decode the String using Base64 again and save the file back to sdcard. It all sounds fairly simple and works great when doing it using a text file. For example if I create a simple text file, call it dave.text and inject some text it in say "Hello Dave" or something similar it works great, but fails when I try doing the same with a binary file, audio in this example. Here's the code that I am using.</p>
  
  <pre><code>File file = new File(Environment.getExternalStorageDirectory() + "/hello-4.wav");
  byte[] FileBytes = FileUtils.readFileToByteArray(file);
  
  byte[] encodedBytes = Base64.encode(FileBytes, 0);
  String encodedString = new String(encodedBytes);                                        
  Utilities.log("~~~~~~~~ Encoded: ", new String(encodedString));
  
  byte[] decodedBytes = Base64.decode(encodedString, 0);
  String decodedString = new String(decodedBytes);
  Utilities.log("~~~~~~~~ Decoded: ", new String(decodedString));
  
  try {
      File file2 = new File(Environment.getExternalStorageDirectory() + "/hello-5.wav");
      FileOutputStream os = new FileOutputStream(file2, true);
      os.write(decodedString.getBytes());
      os.flush();
      os.close();
  } catch (Exception e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
  }
  </code></pre>
  
  <p>At this point if I try to save the file, it becomes corrupted. The audio file hello-5.wav is larger in size then the original and it doesn't play.</p>
  
  <p>Any ideas what I am doing wrong here? If I try to save decodedBytes using os.write(decodedBytes) it works but not when converted to a String and getBytes() is used.</p>
  
  <p>Any ideas? Thank You!</p>