1 import android.app.Activity; 2 import android.content.Intent; 3 import android.content.pm.PackageManager; 4 import android.graphics.Bitmap; 5 import android.os.Bundle; 6 import android.provider.MediaStore; 7 import android.view.View; 8 import android.view.View.OnClickListener; 9 import android.widget.Button; 10 import android.widget.ImageView; 11 12 /* 13 * 调用系统拍照activity来拍照 14 */ 15 public class Main extends Activity implements OnClickListener { 16 17 private ImageView imageView; 18 19 //用系统activity拍照 第1步,检测Android设备是否支持照相机 20 private boolean checkCameraHardware(){ 21 if (getPackageManager().hasSystemFeature( 22 PackageManager.FEATURE_CAMERA)){ 23 return true; 24 }else{ 25 return false; 26 } 27 } 28 /* 29 *拍照按钮事件 30 */ 31 public void onClick(View view) { 32 33 //用系统activity拍照 第2步,准备Intent 34 Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 35 //用系统activity拍照 第3步,启动activity,注意是startActivityForResult 36 startActivityForResult(intent, 1); 37 } 38 39 /* 40 *从系统拍照activity返回的函数 41 */ 42 @Override 43 protected void onActivityResult(int requestCode, int resultCode, Intent data) { 44 //用系统activity拍照 第3步,处理拍照结果 45 if (requestCode == 1) { 46 if (resultCode == Activity.RESULT_OK) { 47 //用系统activity拍照 第4步,取出图片数据 48 Bitmap cameraBitmap = (Bitmap) data.getExtras().get("data"); 49 imageView.setImageBitmap(cameraBitmap); 50 } 51 } 52 } 53 54 @Override 55 public void onCreate(Bundle savedInstanceState) { 56 super.onCreate(savedInstanceState); 57 setContentView(R.layout.main); 58 59 Button btnTakePicture = (Button) findViewById(R.id.btnTakePicture); 60 btnTakePicture.setOnClickListener(this); 61 62 imageView = (ImageView) findViewById(R.id.imageview); 63 } 64 }