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

chethankumar
  • 409
  • 2
  • 5
  • 15
  • 1
    what error did you get? – Gavriel Jan 21 '16 at 12:36
  • am successfully to convert video to byte array, am not getting how to convert byte array to video, can you help me.? – chethankumar Jan 21 '16 at 12:40
  • Where's the code for that (that works for small files)? What's the error message when you have a 50MB file? – Gavriel Jan 21 '16 at 12:41
  • 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. – chethankumar Jan 21 '16 at 12:44
  • 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) – Anubhav Dinkar Apr 15 '20 at 07:48

2 Answers2

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**

chethankumar
  • 409
  • 2
  • 5
  • 15
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);
    }
}
Community
  • 1
  • 1
Gavriel
  • 18,880
  • 12
  • 68
  • 105
  • 1
    How to convert and the play video byte array, can you suggest.? – chethankumar Jan 21 '16 at 13:29
  • 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. – Gavriel Jan 21 '16 at 13:36
  • I found [**this**](https://www.admios.com/blog/how-to-split-a-file-using-java) 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**](https://docs.oracle.com/javase/7/docs/api/java/nio/channels/package-summary.html). – CoDe Jan 12 '20 at 06:58