How to convert video/audio file to byte array and vice versa in android.?
Asked Answered
A

2

5

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;
}

Kindly help out to get the result

Aleut answered 21/1, 2016 at 11:56 Comment(5)
what error did you get?Glume
am successfully to convert video to byte array, am not getting how to convert byte array to video, can you help me.?Aleut
Where's the code for that (that works for small files)? What's the error message when you have a 50MB file?Glume
in the above code only to convert video to byte if its large file gives the error(OutOfMemorryError). but i need to convert byte array to mp4 (video) file.Aleut
I know this is a bit older, but can anyone lead me to any resource that shows how exactly this conversion works internally? (not code, but the internal working of the code instead)Kirin
A
6

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 contains the path, use that to play your audio/video file**

Aleut answered 22/1, 2016 at 6:27 Comment(1)
what is tempMp3 variable?Yolande
G
3

The ByteArrayOutputStream you create is kept in the memory. If you work with huge files, then your memory can limit your ability. This: java.lang.OutOfMemoryError: Java heap space question has a solution that might work for you, though it's probably not the best thing to keep 50MB in the memory.

To answer your other question, you can do the exact same thing:

public void convert(byte[] buf, String path) throws IOException {
    ByteArrayInputStream bis = new ByteArrayInputStream(buf);
    FileOutputStream fos = new FileOutputStream(path);
    byte[] b = new byte[1024];

    for (int readNum; (readNum = bis.read(b)) != -1;) {
        fos.write(b, 0, readNum);
    }
}
Glume answered 21/1, 2016 at 12:52 Comment(3)
How to convert and the play video byte array, can you suggest.?Aleut
No, this question was about the conversion. If you want to play it you can search stackoverflow and you'll find tens of examples for it.Glume
I found this written by ARISTIDES in very detail. He has explained multiple way to do it alongside benchmark result, isn't this so kind of him ! Best way he found is to use CHANNELS.Stinnett

© 2022 - 2024 — McMap. All rights reserved.