Sample code,实现简单应用界面包含两个button,点击button 分别打开camera, 和cameroder,主界面如下图所示
1. 在AndroidManifest.xml文件中包含camera 相关的权限
- AndroidManifest.xml
- <?xml version= "1.0" encoding ="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="android.study.sample"
- android:versionCode= "1"
- android:versionName= "1.0" >
- <uses-sdk android:minSdkVersion= "15" />
- <uses-permission android:name= "android.permission.CAMERA" />
- <uses-permission android:name= "android.permission.WRITE_EXTERNAL_STORAGE" />
- <application
- android:icon="@drawable/ic_launcher"
- android:label="@string/app_name" >
- <activity
- android:name="android.study.sample.CameraIntentActivity"
- android:label="@string/app_name" >
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
- </application >
- </manifest>
2.在主界面的layout文件中添加两个button,分别负责打开拍照和录视频的应用
- -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
- res/layout/main.xml
- <? xml version= "1.0" encoding = "utf-8"?>
- < RelativeLayout xmlns:android ="http://schemas.android.com/apk/res/android"
- android:layout_width= "match_parent"
- android:layout_height= "match_parent" >
- <Button
- android:id ="@+id/buttonTakePic"
- android:layout_width ="match_parent"
- android:layout_height ="wrap_content"
- android:layout_alignParentTop ="true"
- android:layout_centerHorizontal ="true"
- android:layout_marginTop ="100dp"
- android:text ="takepicture" />
- <Button
- android:id ="@+id/buttonRecordVideo"
- android:layout_width ="match_parent"
- android:layout_height ="wrap_content"
- android:layout_alignParentLeft ="true"
- android:layout_alignParentTop ="true"
- android:layout_marginTop ="42dp"
- android:text ="recordvideo" />
- </ RelativeLayout>
3.通过发送不同的Intent打开拍照和录视频的应用
MediaStore.ACTION_IMAGE_CAPTURE MediaStore.ACTION_VIDEO_CAPTURE 程序调用startActivityForResult(Intent, int, Bundle)
接口启动相应的应用程序, 并且必须Activity实现onActivityResult()
来接受调用相应的Camera应用activity之后的结果
- ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
- android.study.sample/CameraIntentActivity.java
- package android.study.sample;
- 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.util.Log;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.Toast;
- public class CameraIntentActivity extends Activity{
- public static final int MEDIA_TYPE_IMAGE = 1;
- public static final int MEDIA_TYPE_VIDEO = 2;
- private static final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 100;
- private static final int CAPTURE_VIDEO_ACTIVITY_REQUEST_CODE = 200;
- private Uri fileUri ;
- Button takePicureButton;
- Button recordVideoButton;
- OnClickListener cameraButtonListenser = new OnClickListener(){
- public void onClick(View view) {
- // TODO Auto-generated method stub
- Intent intent = null;
- if(((Button)view).equals(takePicureButton )){
- // create Intent to take a picture and return control to the calling application
- intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
- fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE); // create a file to save the p_w_picpath
- intent.putExtra(MediaStore. EXTRA_OUTPUT, fileUri ); // set the p_w_picpath file name
- // start the video record Intent
- startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE );
- } else if (((Button)view).equals(recordVideoButton)){
- intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
- fileUri = getOutputMediaFileUri(MEDIA_TYPE_VIDEO); // create a file to save the video
- intent.putExtra(MediaStore. EXTRA_OUTPUT, fileUri ); // set the video file name
- startActivityForResult(intent, CAPTURE_VIDEO_ACTIVITY_REQUEST_CODE );
- }
- }
- };
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout. main);
- takePicureButton = (Button)findViewById(R.id.buttonTakePic);
- recordVideoButton = (Button)findViewById(R.id.buttonRecordVideo);
- takePicureButton.setOnClickListener(cameraButtonListenser );
- recordVideoButton.setOnClickListener(cameraButtonListenser );
- }
- @Override
- protected void onActivityResult(int requestCode, int resultCode, Intent data) {
- if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
- if (resultCode == RESULT_OK) {
- // Image captured and saved to fileUri specified in the Intent
- if(data!=null ){
- Toast. makeText(this, "Image saved to:\n" +
- data.getData().toString(), Toast.LENGTH_LONG).show();
- } else{
- System. out.println("can't get any data!!!" );
- }
- } else if (resultCode == RESULT_CANCELED) {
- // User cancelled the p_w_picpath capture
- } else {
- // Image capture failed, advise user
- }
- }
- if (requestCode == CAPTURE_VIDEO_ACTIVITY_REQUEST_CODE) {
- if (resultCode == RESULT_OK) {
- // Video captured and saved to fileUri specified in the Intent
- if(data!=null ){
- Toast. makeText(this, "Image saved to:\n" +
- data.getData().toString(), Toast.LENGTH_LONG).show();
- } else{
- System. out.println("can't get any data!!!" );
- }
- } else if (resultCode == RESULT_CANCELED) {
- // User cancelled the video capture
- } else {
- // Video capture failed, advise user
- }
- }
- }
- /** Create a file Uri for saving an p_w_picpath or video */
- private static Uri getOutputMediaFileUri(int type){
- return Uri.fromFile( getOutputMediaFile(type));
- }
- /** Create a File for saving an p_w_picpath or video */
- private static File getOutputMediaFile(int type){
- // To be safe, you should check that the SDCard is mounted
- // using Environment.getExternalStorageState() before doing this.
- File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
- Environment. DIRECTORY_PICTURES), "MyCameraApp" );
- // This location works best if you want the created p_w_picpaths to be shared
- // between applications and persist after your app has been uninstalled.
- // Create the storage directory if it does not exist
- if (! mediaStorageDir.exists()){
- if (! mediaStorageDir.mkdirs()){
- Log. d("MyCameraApp", "failed to create directory");
- return null ;
- }
- }
- // Create a media file name
- String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss" ).format(new Date());
- File mediaFile;
- if (type == MEDIA_TYPE_IMAGE){
- mediaFile = new File(mediaStorageDir.getPath() + File.separator +
- "IMG_"+ timeStamp + ".jpg" );
- } else if (type == MEDIA_TYPE_VIDEO) {
- mediaFile = new File(mediaStorageDir.getPath() + File.separator +
- "VID_"+ timeStamp + ".mp4" );
- } else {
- return null ;
- }
- return mediaFile;
- }
- }