Directly call the System camera

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.