如何转换视频为Base64数据 [英] How to convert video to base64 data

查看:10632
本文介绍了如何转换视频为Base64数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,采取视频从相机或画廊,并转换成的base64数据和数据发送到服务器,但问题是,每当我base64转换数据将是在可视数据变量不正确的数据。为了这个,我低于code使用的:

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

请使其正确的...

推荐答案

在连接code数据的 byteBufferString ,要在编码只有最后一块读。你应该带code的 ByteArrayOutputStream 的全部内容。你可以用下面的code做到这一点:

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

不过,有一个机会,这可能会引发的OutOfMemoryError 如果视频大小大。

这篇关于如何转换视频为Base64数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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