base64编码音频文件并作为String发送然后解码字符串 [英] base64 encode audio file and send as a String then decode the String
问题描述
我已经坚持这个问题几个小时了,现在试图让它运转起来。基本上我想要做的是以下内容。 Base64编码从Android设备上的SD卡中拾取的音频文件,Base64对其进行编码,将其转换为字符串,然后再次使用Base64解码字符串并将文件保存回sdcard。听起来相当简单,使用文本文件时效果很好。例如,如果我创建一个简单的文本文件,调用它dave.text并注入一些文本,说Hello Dave或类似的东西它工作得很好,但是当我尝试用二进制文件做同样的事情时失败,在这个例子中是音频。这是我正在使用的代码。
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.
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的大小比原始文件大,而且没有播放。
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.
任何想法我在这里做错了什么?如果我尝试使用os.write(decodingBytes)保存decodeBytes,它可以工作但不能转换为String并使用getBytes()。
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.
有什么想法吗?谢谢!
推荐答案
你正在大量混合 String
s和字节[]
秒。不要那样做。如果要将 byte []
编码为 String
,请使用 Base64.encodeToString()
,而不是编码为字节,然后然后创建一个字符串。
You're heavily mixing String
s and byte[]
s. Don't do that. If you want to encode a byte[]
to a String
, use Base64.encodeToString()
, instead of encoding to bytes and then creating a string.
如果我尝试使用os.write(decodingBytes)保存decodeBytes它可以工作,但是当转换为String并且没有使用getBytes()时。
If I try to save decodedBytes using os.write(decodedBytes) it works but not when converted to a String and getBytes() is used.
调用 new String(byte [])
不能达到您的预期。
Calling new String(byte[])
does not do what you think it does.
同样使用 Base64.decode (String,int)
解码字符串。
Likewise use Base64.decode(String, int)
to decode the string.
像这样(未经测试):
File file = new File(Environment.getExternalStorageDirectory() + "/hello-4.wav");
byte[] bytes = FileUtils.readFileToByteArray(file);
String encoded = Base64.encodeToString(bytes, 0);
Utilities.log("~~~~~~~~ Encoded: ", encoded);
byte[] decoded = Base64.decode(encoded, 0);
Utilities.log("~~~~~~~~ Decoded: ", Arrays.toString(decoded));
try
{
File file2 = new File(Environment.getExternalStorageDirectory() + "/hello-5.wav");
FileOutputStream os = new FileOutputStream(file2, true);
os.write(decoded);
os.close();
}
catch (Exception e)
{
e.printStackTrace();
}
但为什么你是base64首先编码音频文件?
But why are you base64 encoding an audio file in the first place?
这篇关于base64编码音频文件并作为String发送然后解码字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!