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

chethankumar

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

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to convert POJO to Map and vice versa in Java?

How to convert a netty ByteBuf to a String and vice versa

how to convert joda datetime to String and vice versa

How to convert byte array to string and vice versa?

Convert a byte array to integer in Java and vice versa

Put byte array to JSON and vice versa

Convert file to byte array and vice versa

How to convert a float into a byte array and vice versa?

byte to string and vice versa

How can I convert tabs to spaces and vice versa in an existing file

How do you convert a byte array to a hexadecimal string, and vice versa?

How to convert String[] to String and vice versa in Android

How can I convert double-float to byte array and vice versa in common lisp?

How to convert Drawable into int and vice versa in Android

Cannot convert String to byte array and vice versa in Java

BitmapImage to byte array and vice versa

Reading objects into array from file and vice versa

How to convert AnsiString to TBytes and vice versa?

Converting String to Byte Array and vice versa

How to convert double byte character/string to single byte and vice versa?

How to convert CLOB to image or vice versa?

What is the most efficient way to convert array of int to array of byte and vice versa?

How to convert integers to pointers and vice versa

How to convert the excel file format xls and xlsx vice versa in java

Convert struct array to cell of structs and vice versa

ByteBuffer - convert long to char array and vice versa

How to convert string into numpy array for tensorflow and vice versa

How to convert entity to dto, and vice versa?

How to convert numpy array to string and vice versa