1.首先在布局文件中定义两个按钮
2.开启摄像头或者选择本地库
private final int IMAGE_RESULT_CODE = 1;//拍照意图回传值结果码</span>
private final int IMAGE_RESULT_OPEN_CODE = 2;//选择本地库
private ImageView imageView;
protected void onCreate(Bundle savedInstanceState) {
<span > </span>Button btn1 = (Button)this.findViewById(R.id.button1);
<span > </span>Button btn2 = (Button)this.findViewById(R.id.button2);
<span > </span>btn1.setOnClickListener(new View.OnClickListener(){
<span > </span>@Override</span>
<span > </span>public void onClick(View v){ <span style="font-family: Arial, Helvetica, sans-serif;"> </span>
<span style="font-family: Arial, Helvetica, sans-serif;"><span > </span>//拍照意图</span>
Intent intent = new Intent(android.provider.MediaStore.ACTION_VIDEO_CAPTURE);
startActivityForResult(intent, IMAGE_RESULT_CODE);
<span > </span>}
<span > </span>});
btn2.setOnClickListener(new View.OnClickListener(){
<span > </span>@Override
<span > </span>public void onClick(View v){
<span > </span>//选择图片库的图片
Intent intent2 = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent2, IMAGE_RESULT_OPEN_CODE);
<span > </span>}
<span > </span>});
<span > </span>imageView = (ImageView)this.findViewById(R.id.icon_people_imgview);
<span > </span> }
<span > </span>
<span > </span>
<span > </span>//activity返回操作
<span > </span>@Override
<span > </span>protected void onActivityResult(int requestCode, int resultCode, Intent data) {
<span > </span>//表示拍照返回后的显示结果
<span > </span>if(requestCode == 1 && resultCode == RESULT_OK){
<span > </span>Bundle bundle = data.getExtras();
<span > </span>Bitmap bitmap = (Bitmap)bundle.get("data");
<span > </span>imageView.setImageBitmap(bitmap);
<span > </span>//表示选择本地图片库后的图片显示结果
<span > </span>} else if(requestCode == 2 && resultCode == RESULT_OK) {
<span > </span>imageView.setImageURI(data.getData());
<span > </span>}
<span > </span>super.onActivityResult(requestCode, resultCode, data);
<span > </span>}
OK了,然后不管是拍摄的照片,还是本地库选择的图片,就会被陈列在ImageView控件上
那么接下来的工作就该是图片上上传了
标签:拍照,CODE,图片库,void,Intent,RESULT,imageView,android,data From: https://blog.51cto.com/u_14062833/6087819