【发布时间】:2018-07-22 22:28:25
【问题描述】:
我已经在这个问题上停留了几个小时,现在试图让它工作。基本上我想做的是以下几点。 Base64 编码从 Android 设备上的 sdcard 拾取的音频文件,Base64 对其进行编码,将其转换为字符串,然后再次使用 Base64 解码字符串并将文件保存回 sdcard。这一切听起来相当简单,并且在使用文本文件时效果很好。例如,如果我创建一个简单的文本文件,将其命名为 dave.text 并在其中注入一些文本,比如“Hello Dave”或类似的内容,它效果很好,但是当我尝试对二进制文件(本例中的音频)执行相同操作时失败.这是我正在使用的代码。
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();
}
此时如果我尝试保存文件,它会损坏。音频文件 hello-5.wav 比原始文件大,无法播放。
有什么想法我在这里做错了吗?如果我尝试使用 os.write(decodedBytes) 保存 decodedBytes 它可以工作,但在转换为 String 并使用 getBytes() 时不起作用。
有什么想法吗?谢谢!
【问题讨论】: