概述
使用Camera有两种方式:通过Intent方式和通过Camera的API。在开发系列(一) 中已经介绍了通过Intent方式拍照,本文介绍通过Intent的方式录制视频。
通过Itent实现拍录制视频
第一步:在Eclipse中创建一个名为AndroidCamera的Android工程,可参见Helloworld的例子;
第二步:在AndroidManifest.xml中添加使用Camera相关的声明如下:
- <uses-feature android:name="android.hardware.camera" android:required="false" />
- <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
-
第三步
:编写
AndroidCameraActivity
类,如下:
\
- import java.io.File;
- import java.text.SimpleDateFormat;
- import java.util.Date;
- import android.app.Activity;
- import android.content.Intent;
- import android.net.Uri;
- import android.os.Bundle;
- import android.os.Environment;
- import android.provider.MediaStore;
- import android.widget.Toast;
-
- public class AndroidCameraActivity extends Activity {
- private static final int CAPTURE_VIDEO_ACTIVITY_REQUEST_CODE = 200;
-
- private Intent intent = null;
- private Uri fileUri = null;
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
-
- intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
- fileUri = getOutputMediaFileUri();
-
-
- intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
-
-
- intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
-
-
- startActivityForResult(intent, CAPTURE_VIDEO_ACTIVITY_REQUEST_CODE);
- }
-
- @Override
- protected void onActivityResult(int requestCode, int resultCode, Intent data) {
- super.onActivityResult(requestCode, resultCode, data);
-
- if(requestCode == CAPTURE_VIDEO_ACTIVITY_REQUEST_CODE) {
- if (resultCode == RESULT_OK) {
-
- Toast.makeText(this, "Video saved to:\n" +
- data.getData(),
- Toast.LENGTH_LONG).show();
- } else if (resultCode == RESULT_CANCELED) {
-
- }
- }
- }
-
-
- private static Uri getOutputMediaFileUri(){
-
- File picDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
-
-
- String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
-
- File videoFile = new File(picDir.getPath() + File.separator + "VIDEO_"+ timeStamp + ".mp4");
-
- return Uri.fromFile(videoFile);
- }
- }
第四步:运行程序。
运行程序会出现录制视频的窗口,录制保存后可以即可在SD卡的Pictures目录下找到刚才录制的视频。
参考资料
- http://developer.android.com/guide/topics/media/camera.html