我得到了这个代码,用户可以在其中从图库中选择视频或拍摄视频:
private void showPictureDialog(){
AlertDialog.Builder pictureDialog = new AlertDialog.Builder(this);
pictureDialog.setTitle("Select Action");
//从图库中挑选或拍摄视频
String[] pictureDialogItems = {
"Select video from gallery",
"Record video from camera" };
pictureDialog.setItems(pictureDialogItems,
new DialogInterface.OnClickListener() {
@Override
//用户点击其中一个选项
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case 0:
chooseVideoFromGallary();
break;
case 1:
takeVideoFromCamera();
break;
}
}
});
pictureDialog.show();
}
//intent 根据用户的选择
public void chooseVideoFromGallary() {
Intent galleryIntent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Video.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(galleryIntent, GALLERY);
}
private void takeVideoFromCamera() {
Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
startActivityForResult(intent, CAMERA);
}
// 然后,根据活动结果,它会执行我需要的所有操作,包括查找 uri 和路径。一切正常,我可以在 videoView 上看到拍摄/挑选的视频。
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.d("result",""+resultCode);
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == this.RESULT_CANCELED) {
Log.d("what","cancle");
return;
}
if (requestCode == GALLERY) {
Log.d("what","gale");
if (data != null) {
Uri contentURI = data.getData();
String selectedVideoPath = getPath(contentURI);
Log.d("path",selectedVideoPath);
saveVideoToInternalStorage(selectedVideoPath);
File currentFile = new File(selectedVideoPath);
String currentFileS = currentFile.toString();
videoView.setVideoURI(contentURI);
videoView.requestFocus();
videoView.start();
}
} else if (requestCode == CAMERA) {
Uri contentURI = data.getData();
String recordedVideoPath = getPath(contentURI);
Log.d("frrr",recordedVideoPath);
saveVideoToInternalStorage(recordedVideoPath);
videoView.setVideoURI(contentURI);
videoView.requestFocus();
videoView.start();
}
}
我需要使用选定的视频数据获取字节数组。我不确定如何。 Tnx