I got stuck on this issue for several hours trying to get it to work. Basically I am trying to do the following. Base64 encodes the audio file received from the SD card on the Android device, Base64 encodes it, converts it to String and then decodes the String again with Base64 and saves the file back to the SD card. All this sounds pretty simple and works great when using a text file. For example, if I create a simple text file, name it dave.text and add the text to "Hello Dave" or something similar, it works fine, but it fails when I try to do the same with the binary file, the audio in this example, Here is the code that I use.
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) {
At this point, if I try to save the file, it will be damaged. The sound file hello-5.wav is larger than the original, and it does not play.
Any ideas what I'm doing wrong here? If I try to save decodedBytes using os.write (decodedBytes), it works, but not when conversion to String and getBytes () is used.
Any ideas? Thanks!
ddavtian
source share