How to convert video to basic data

I have an application that takes video from a camera or gallery and converts it to base64 data, and this data is sent to the server, but the problem is that whenever I convert base64 data, it will be incorrect data in the video data variable. For this, I used the code below:

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 do it right ...

+7
source share
2 answers

When you encode byteBufferString , you encode only the last piece of data read. You must encode all the contents of ByteArrayOutputStream . You can do this with the following code:

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

However, there is a chance that this could cause OutOfMemoryError if the video size is large.

+5
source

I solve it as follows:

 @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 strong> has base64 video. I hope you serve, I worked great

+1
source

All Articles