1、调用系统的照相机

 public void click(View view) {
     // 激活系统的照相机拍照
     Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
     intent.addCategory("android.intent.category.DEFAULT");
     intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File("/sdcard/test.jpg")));
     startActivity(intent);
 }

2、自定义照相机

2.1 实例化Camera

2.2 在事件中调用Camera中的takePicture(ShutterCallback shutter, PictureCallback raw, PictureCallback jpeg)方法

2.3 在PictureCallback回调接口实现保存照片

 public class MainActivity extends ActionBarActivity {
     private Camera mCamera;
     private CameraPreview mPreview;

     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);
         // Create an instance of Camera
         mCamera = getCameraInstance();

         // Create our Preview view and set it as the content of our activity.
         mPreview = new CameraPreview(this, mCamera);
         FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
         preview.addView(mPreview);

         // Add a listener to the Capture button
         Button captureButton = (Button) findViewById(R.id.button_capture);
         captureButton.setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View v) {
                 mCamera.autoFocus(new AutoFocusCallback() {
                     @Override
                     public void onAutoFocus(boolean success, Camera camera) {
                         mCamera.takePicture(null, null, mPicture);
                         // mCamera.startPreview();
                     }
                 });
             }
         });
     }

     /** A safe way to get an instance of the Camera object. */
     public static Camera getCameraInstance() {
         Camera c = null;
         try {
             c = Camera.open(); // attempt to get a Camera instance
         } catch (Exception e) {
             // Camera is not available (in use or does not exist)
         }
         return c; // returns null if camera is unavailable
     }

     private PictureCallback mPicture = new PictureCallback() {
         @Override
         public void onPictureTaken(byte[] data, Camera camera) {
             File pictureFile = new File("/sdcard/aaaa"
                     + System.currentTimeMillis() + ".jpg");// getOutputMediaFile(MEDIA_TYPE_IMAGE);
             try {
                 FileOutputStream fos = new FileOutputStream(pictureFile);
                 fos.write(data);
                 fos.close();
             } catch (Exception e) {
                 Log.d("TAG", "Error accessing file: " + e.getMessage());
             }
         }
     };

     protected void onDestroy() {
         if (mCamera != null) {
             mCamera.release();
             mCamera = null;
         }
     };
 }

配置文件activity_main.xml

 <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:orientation="horizontal"
     android:layout_width="fill_parent"
     android:layout_height="fill_parent"
     >
   <FrameLayout
     android:id="@+id/camera_preview"
     android:layout_width="fill_parent"
     android:layout_height="fill_parent"
     android:layout_weight="1"
     />

   <Button
     android:id="@+id/button_capture"
     android:text="Capture"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_gravity="center"
     />
 </LinearLayout>
05-19 12:20