Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert video to base64 data

I have an app that take video from camera or gallery and convert it into base64 data and that data send to server but the problem is whenever I convert base64 data it will be not correct data in videodata variable. for this I used below code :

FileInputStream objFileIS = null;
try
{
    System.out.println("file = >>>> <<<<<" + selectedImagePath);
    objFileIS = new FileInputStream(selectedImagePath);
} 
catch (FileNotFoundException e) 
{
    e.printStackTrace();
}
ByteArrayOutputStream objByteArrayOS = new ByteArrayOutputStream();
byte[] byteBufferString = new byte[1024];
try
{
    for (int readNum; (readNum = objFileIS.read(byteBufferString)) != -1;) 
    {
        objByteArrayOS.write(byteBufferString, 0, readNum);
        System.out.println("read " + readNum + " bytes,");
    }
} 
catch (IOException e)
{
    e.printStackTrace();
}                    

videodata = Base64.encodeToString(byteBufferString, Base64.DEFAULT);
Log.d("VideoData**>  " , videodata);

Please make it correct...

like image 934
Mukesh Parmar Avatar asked May 22 '13 05:05

Mukesh Parmar


People also ask

Can we convert video to Base64?

You can do this with the following code: videodata = Base64.

How do I convert to Base64?

Convert Files to Base64 Just select your file or drag & drop it below, press the Convert to Base64 button, and you'll get a base64 string. Press a button – get base64. No ads, nonsense, or garbage. The input file can also be an mp3 or mp4.

What is Base64 encoding data?

Base64 is a group of similar binary-to-text encoding schemes that represent binary data in an ASCII string format by translating it into a radix-64 representation. The term Base64 originates from a specific MIME content transfer encoding.


1 Answers

When you encode the byteBufferString, you are encoding only the last chunk of data read. You should encode the whole contents of the ByteArrayOutputStream. You can do this with the following code:

videodata = Base64.encodeToString(objByteArrayOS.toByteArray(), Base64.DEFAULT);

However, there is a chance that this may throw an OutOfMemoryError if the video size is big.

like image 95
Rajesh Avatar answered Oct 08 '22 17:10

Rajesh