如何将视频转换为base64数据

时间:2013-05-22 05:45:30

标签: android video base64

我有一个应用程序,从相机或图库中获取视频并将其转换为base64数据,并将数据发送到服务器,但问题是每当我转换base64数据时,它将不是videodata变量中的正确数据。为此我用下面的代码:

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

请改正......

3 个答案:

答案 0 :(得分:5)

byteBufferString进行编码时,只编码最后读取的数据块。您应该编码ByteArrayOutputStream的全部内容。您可以使用以下代码执行此操作:

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

但是,如果视频尺寸很大,则有可能会抛出OutOfMemoryError

答案 1 :(得分:1)

我这样解决:

@Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        Log.d("Imagen este ", String.valueOf(data));
        if(data!=null) {
            switch (requestCode) {
case SELECT_VIDEO:
                    Uri selectedVideoUri = data.getData();
                    String[] projection = {MediaStore.Video.Media.DATA, MediaStore.Video.Media.SIZE, MediaStore.Video.Media.DURATION};
                    Cursor cursor = managedQuery(selectedVideoUri, projection, null, null, null);

                    cursor.moveToFirst();
                    String filePath = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATA));
                    Log.d("File Name:",filePath);

                    Bitmap thumb = ThumbnailUtils.createVideoThumbnail(filePath, MediaStore.Video.Thumbnails.MINI_KIND);
                    // Setting the thumbnail of the video in to the image view
                    msImage.setImageBitmap(thumb);
                    InputStream inputStream = null;
// Converting the video in to the bytes
                    try
                    {
                        inputStream = getContentResolver().openInputStream(selectedVideoUri);
                    }
                    catch (FileNotFoundException e)
                    {
                        e.printStackTrace();
                    }
                    int bufferSize = 1024;
                    byte[] buffer = new byte[bufferSize];
                    byteBuffer = new ByteArrayOutputStream();
                    int len = 0;
                    try
                    {
                        while ((len = inputStream.read(buffer)) != -1)
                        {
                            byteBuffer.write(buffer, 0, len);
                        }
                    }
                    catch (IOException e)
                    {
                        e.printStackTrace();
                    }
                    System.out.println("converted!");

                    String videoData="";
                    //Converting bytes into base64
                    videoData = Base64.encodeToString(byteBuffer.toByteArray(), Base64.DEFAULT);
                    Log.d("VideoData**>  " , videoData);

                    String sinSaltoFinal2 = videoData.trim();
                    String sinsinSalto2 = sinSaltoFinal2.replaceAll("\n", "");
                    Log.d("VideoData**>  " , sinsinSalto2);

                    baseVideo = sinsinSalto2;

videoData 有视频的base64 ..我希望你发球,我工作完美

答案 2 :(得分:0)

试试这个代码,把它粘贴到“.html”中,看看你是否喜欢它放在 console.log 中的内容:

<input type="file" accept="video/*" oninput="uploadFile(this)" />

function uploadFile(event) {
    var file = event.files[0];
    var reader = new FileReader();
    reader.onload = (event) => {
        var media = event.target.result;

        console.log(media);
    };
    reader.readAsDataURL(file);
}