【问题标题】:base64 encode audio file and send as a String then decode the Stringbase64 编码音频文件并作为字符串发送,然后解码字符串
【发布时间】: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() 时不起作用。

有什么想法吗?谢谢!

【问题讨论】:

    标签: java android


    【解决方案1】:

    您将Strings 和byte[]s 混为一谈。不要那样做。如果要将byte[] 编码为String,请使用Base64.encodeToString(),而不是编码为字节并然后创建一个字符串。

    如果我尝试使用 os.write(decodedBytes) 保存 decodedBytes,它可以工作,但在转换为 String 并使用 getBytes() 时不起作用。

    调用new String(byte[]) 并没有像你想象的那样做。

    同样使用Base64.decode(String, int) 来解码字符串。

    类似这样的东西(未测试):

    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 编码呢?

    【讨论】:

    • 谢谢马特,完美的解决方案。我相信这会对很多人有所帮助!
    • 嗨,马特,我遇到了同样的问题。你建议这是一种播放音频的写入方式。 bcoz 播放音频需要很长时间。首先我读取加密文件然后解密它并再次写回mp4文件格式。
    • 我想知道为什么您和操作员在输出流中使用了“真”-FileOutputStream(file2, true); 根据文档,这将从文件末尾开始写,我猜这不会输出文件是否还不存在并不重要,或者是吗?我认为在 op 的原始帖子中,这可能是他至少获得更大文件的原因
    • @Wraithious 你得问问 OP:我只是修改了现有的代码。
    【解决方案2】:

    将base64字符串文件解码为.m4a文件格式

    try
    {
        String audioDataString = "";
        BufferedReader jsonReader = new BufferedReader(new InputStreamReader(this.getResources().openRawResource(R.raw.audioBase64File)));
        StringBuilder jsonBuilder = new StringBuilder();
        for (String line = null; (line = jsonReader.readLine()) != null; ) {
            jsonBuilder.append(line).append("");
        }
        audioDataString = jsonBuilder.toString();
        byte[] decoded = Base64.decode(audioDataString, Base64.DEFAULT);
        try {
            File file2 = new File(Environment.getExternalStorageDirectory() + "/faakhir_testAudio.m4a");
            FileOutputStream os = new FileOutputStream(file2, true);
            os.write(decoded);
            os.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    } catch(Exception e){
        e.printStackTrace();
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-13
      相关资源
      最近更新 更多