如何在Android中将视频/音频文件转换为字节数组,反之亦然? [英] How to convert video/audio file to byte array and vice versa in android.?

查看:200
本文介绍了如何在Android中将视频/音频文件转换为字节数组,反之亦然?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Am试图将音频/视频转换为字节数组,反之亦然, 使用下面的代码能够将音频/视频文件转换为字节数组(请参见下面的代码),但是无法转换大文件(超过50MB的文件)是否有任何限制. 如何将字节数组转换为音频/视频文件?请帮我.

Am trying to convert audio/video to byte array and vice versa, using below code am able converting audio/video files to byte array(see below code) but am fail to convert large file(more then 50MB files) is there any limit.? how to convert byte array to audio/video file.? kindly help me out.

public byte[] convert(String path) throws IOException {

    FileInputStream fis = new FileInputStream(path);
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    byte[] b = new byte[1024];

    for (int readNum; (readNum = fis.read(b)) != -1;) {
        bos.write(b, 0, readNum);
    }

    byte[] bytes = bos.toByteArray();

    return bytes;
}

请提供帮助

推荐答案

感谢...在您的帮助下,我得到了解决方案,可以将字节转换为文件(音频/视频),请参见下面的代码.

Thanks... with your help i got solution, to convert the bytes to file(audio/video), see below code.

private void convertBytesToFile(byte[] bytearray) {
    try {

        File outputFile = File.createTempFile("file", "mp3", getCacheDir());
        outputFile.deleteOnExit();
        FileOutputStream fileoutputstream = new FileOutputStream(tempMp3);
        fileoutputstream.write(bytearray);
        fileoutputstream.close();

    } catch (IOException ex) {
        ex.printStackTrace();
    }
}

** File outputFile = File.createTempFile("file", "mp3", getCacheDir());

outputFile包含用于播放音频/视频文件的路径**

outputFile contains the path, use that to play your audio/video file**

这篇关于如何在Android中将视频/音频文件转换为字节数组,反之亦然?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆