今天分享的是用系统自带的相机实现一键拍照功能。
“`
public class MainActivity extends AppCompatActivity {
private static final int TAKE_PHOTO = 100;
private ImageView iv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv = (ImageView) findViewById(R.id.iv);
}
public void btn(View view) {
switch (view.getId()){
case R.id.button:
//跳转到拍照的意图
Intent takePhotoIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
//判断系统中是否有照相机
if (takePhotoIntent.resolveActivity(getPackageManager())!=null){
startActivityForResult(takePhotoIntent,TAKE_PHOTO);
}
break;
}
}
/**
* 处理数据
* @param requestCode
* @param resultCode
* @param data
*/
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
//判断请求码和返回码
if (requestCode==TAKE_PHOTO&&resultCode==RESULT_OK){
Bitmap bitmap = data.getParcelableExtra("data");
iv.setImageBitmap(bitmap);
}
}
}
“`