Android 使用系統攝像頭拍相片與拍視訊,並顯示
阿新 • • 發佈:2019-02-07
public class OpenCamaraActivity extends BaseActivity {
private static final int IMAGE_REQUEST_CODE = 0;
private static final int VIDEO_REQUEST_CODE = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public void openImage (View view) {
Intent intent = new Intent();
intent.setAction("android.media.action.IMAGE_CAPTURE");
intent.addCategory("android.intent.category.DEFAULT");
Uri value = Uri.fromFile(new File("/sdcard/image.jpg"));
intent.putExtra(MediaStore.EXTRA_OUTPUT, value);
startActivityForResult(intent, IMAGE_REQUEST_CODE);
}
public void openVideo(View view) {
Intent intent = new Intent();
intent.setAction("android.media.action.VIDEO_CAPTURE");
intent.addCategory("android.intent.category.DEFAULT");
Uri value = Uri.fromFile(new File("/sdcard/good.3gp"));
intent.putExtra(MediaStore.EXTRA_OUTPUT, value);
startActivityForResult(intent, VIDEO_REQUEST_CODE);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (data != null) {
if (requestCode == IMAGE_REQUEST_CODE) {
//開啟圖片
Uri uri = data.getData();
ImageView imageView = new ImageView(this);
imageView.setImageURI(uri);
}else if (requestCode == VIDEO_REQUEST_CODE) {
//開啟視訊
Uri uri = data.getData();
//VideoView videoView = (VideoView)this.findViewById(R.id.video_view);
VideoView videoView = new VideoView(this);
videoView.setMediaController(new MediaController(this));
videoView.setVideoURI(uri);
videoView.start();
videoView.requestFocus();
}
}
super.onActivityResult(requestCode, resultCode, data);
}
}