android 如何调用本地相机拍照

android中调用相机拍照并显示在图片控件上。代码如下

package com.example.camera;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends Activity {
	
	private Button button;
	private ImageView imageView;
	private final int IMAGE_RESULT_CODE = 1;//意图回传值的结果码

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		button = (Button)this.findViewById(R.id.button1);
		imageView = (ImageView)this.findViewById(R.id.imageView1);
		
		button.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				//  使用意图,直接调用在手机上的照相机
				Intent intent =new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
				//打开照相机
				startActivityForResult(intent, IMAGE_RESULT_CODE);
			}
		});
	}
	
	//使用意图的回传值
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, intent);
		if(IMAGE_RESULT_CODE == requestCode && resultCode == RESULT_OK){
			Bundle bundle = intent.getExtras();
			Bitmap bitmap = (Bitmap)bundle.get("data");
			imageView.setImageBitmap(bitmap);
			
		}
	}

}
来源://作者:/更新时间:2015-09-16
相关文章