Key ideas:
Initialize components;
Create and start the photo intent;
Use the onactivityresult () callback function to process images.
Key code:
Initialize components:
takePicBtn = (Button) findViewById(R.id.buttonPicture);takePicBtn.setOnClickListener(takePiClickListener);takeVideoBtn = (Button) findViewById(R.id.buttonVideo);takeVideoBtn.setOnClickListener(takeVideoClickListener);
Create and start the photo intent:
Intent intent = new intent (mediastore. action_image_capture );
Startactivityforresult (intent, capture_image_activity_request_code );
Private Final onclicklistener takepiclicklistener = new view. onclicklistener () {@ override public void onclick (view v) {log. D (log_tag, "take picture button click"); // create a new intent ------ use the camera app that comes with the system: Photograph intent = new intent (mediastore. action_image_capture); // create a file to save the image ------ create a file to save the image fileuri = getoutputmediafileuri (media_type_image ); // The intent value is related to the branch that will be entered in onactivityresult, that is, whether data is null. If this parameter is specified, then the data is null -- 180 lines of code // set the image file name ------ set the image file name intent. putextra (mediastore. extra_output, fileuri); // start intent startactivityforresult (intent, capture_image_activity_request_code );}};
Use the callback function onactivityresult () to process the image:
// If you are taking a photo if (capture_image_activity_request_code = requestcode) {log. D (log_tag, "capture_image"); If (result_ OK = resultcode) {log. D (log_tag, "result_ OK"); If (Data! = NULL) {// log when no specific storage path is specified. D (log_tag, "data is not null, file on default position. ");} else {log. D (log_tag, "data is null, file saved on target position. "); // resize the full image to fit in out Image view. // decode the image file into a bitmap sized to fill the view Bitmap bitmap = bitmapfactory. decodefile (fileuri. getpath (), factoryoptions); imageview. setimagebitmap (Bitmap) ;}} else if (resultcode = result_canceled) {// user canceled the image capture} else {// image capture failed, advise user }}
Directly call the System camera