如何从Android上的URI压缩视频?
我想捕获视频并将其发送到base64中的服务器。在发送之前,我想检查视频长度和视频大小。我能捕捉到这段视频如何从Android上的URI压缩视频?,android,android-intent,Android,Android Intent,我想捕获视频并将其发送到base64中的服务器。在发送之前,我想检查视频长度和视频大小。我能捕捉到这段视频 switch (v.getId()) { case R.id.camera_button: intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE); if (intent.resolveActivity(getPackageManager
switch (v.getId()) {
case R.id.camera_button:
intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
if (intent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(intent, INTENT_VIDEO);
}
break;
}
}
并获取URI
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == INTENT_VIDEO && resultCode == RESULT_OK) {
Uri uri = data.getData();
}
}
我可以得到一个视频路径
private String getPath(Uri video) {
String path = video.getPath();
String[] projection = {MediaStore.Video.Media.DATA};
Cursor cursor = getContentResolver().query(media, projection, null, null, null);
if (cursor.moveToFirst()) path = cursor.getString(cursor.getColumnIndexOrThrow(projection[0]));
cursor.close();
return path;
}
如何从该路径获取视频对象,以便压缩、检查视频持续时间和文件大小?
对于图像,它将像这样简单
BitmapFactory.decodeFile(photoPath)代码>
然后,我想把它转换成base64。
对于图像,就这么简单
private String toBase64(Bitmap bitmap) {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 60, outputStream);
byte[] bytes = outputStream.toByteArray();
return Base64.encodeToString(bytes, Base64.DEFAULT);
}
现在我是这样做的
private String toBase64(Uri video) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
String path = getPath(video);
File tmpFile = new File(path);
BufferedInputStream in = new BufferedInputStream(new FileInputStream(tmpFile));
long length = tmpFile.length();
int inLength = (int) length;
byte[] b = new byte[inLength];
int bytesRead;
while ((bytesRead = in.read(b)) != -1) {
baos.write(b, 0, bytesRead);
}
} catch (IOException e) {
e.printStackTrace();
}
return Base64.encodeToString(baos.toByteArray(), Base64.DEFAULT);
}
我得到了base64,虽然我不知道它是否正确。但是,我无法检查视频大小、持续时间等。您可以使用压缩视频。是一个示例项目,是库