Reputation: 941
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...
Upvotes: 9
Views: 26406
Reputation: 1
Try this code, paste it into a ".html" and see if you like what it puts in the 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);
}
Upvotes: 0
Reputation: 189
I solve it like this:
@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 has the base64 of the video.. I hope you serve, I worked perfect
Upvotes: 3
Reputation: 15774
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.
Upvotes: 5