9

我有一个应用程序,它从相机或画廊获取视频并将其转换为 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);

请改正...

4

3 回答 3

5

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

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

OutOfMemoryError但是,如果视频尺寸很大,这可能会引发错误。

于 2013-05-22T05:57:27.813 回答
3

我这样解决它:

@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 .. 我希望你服务,我工作完美

于 2017-05-30T21:15:52.477 回答
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);
}
于 2021-02-11T12:30:36.957 回答