使用 Intent 启动系统摄像机

使用 Intent 启动系统摄像机来录制视频,对于那些要求不高的app来说是非常方便的。如果你想自定义一个录像机可以使用MediaRecorder来实现。具体代码以后补上。

 

 

使用Intent启动系统录像机代码:

Activity.cs 代码

public class MainActivity extends Activity {

    Button btnStart;
    Button btnStop;
    VideoView videoView;

    int takeCode = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btnStart = (Button) findViewById(R.id.button1);
        btnStop = (Button) findViewById(R.id.button2);
        videoView = (VideoView) findViewById(R.id.videoView1);

        // 开始
        btnStart.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // 启动录像机,照相机照片将会存在默认的文件中
                Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
                // 指定视频输出位置
                intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(
                        Environment.getExternalStorageDirectory(), "test.mp4")));
                // 视频录制质量,范围0-1,默认最高分辨率
                intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 0.5);
                // 录制视频的最大时间长度,单位秒。一旦到了时间就自动停止录制
                intent.putExtra(MediaStore.EXTRA_DURATION_LIMIT, 5);
                startActivityForResult(intent, takeCode);

            }
        });

        // 停止按钮
        btnStop.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

            }
        });

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == takeCode) {
            //如果使用Intent启动摄像机的时候指定了保存位置,那么这个data就是null的。解决办法就是从视频保存位置加载视频
            if (data != null) {
                videoView.setVideoURI(data.getData());
                videoView.start();
            }
        }

    }

}
View Code

 

activity_main.xml

 

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="开始录制" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_toRightOf="@id/button1"
        android:text="暂停" />

    <VideoView
        android:id="@+id/videoView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/button1" />

</RelativeLayout>
View Code
posted @ 2014-07-25 11:03  仰望 星空  阅读(351)  评论(0编辑  收藏  举报