Android calls the system camera to get pictures, call system albums to get pictures, and capture pictures

Source: Internet
Author: User

Open the System album Get Pictures and Intercept, the code is relatively simple

1Intent Intent =NewIntent (Intent.action_get_content,NULL);2Intent.settype ("image/*");3Intent.putextra ("Crop", "true");4 5                 //width and height refer to the width and height ratio of the capture box, if width = 1,height = 1, The Intercept box is square6Intent.putextra ("Aspectx", WIDTH);7Intent.putextra ("Aspecty", HEIGHT);8 9                 //output_x and Output_y refer to the width and height of the picture, which can be set according to the actual needTenIntent.putextra ("Outputx", output_x); OneIntent.putextra ("Outputy", output_y); AIntent.putextra ("scale",true); -  -                 //Return-data refers to whether the data is returned in the Onactivityresult method theIntent.putextra ("Return-data",true); -Intent.putextra ("OutputFormat", Bitmap.CompressFormat.JPEG.toString ()); -  -Intent.putextra ("Nofacedetection",true); +  - //to_gallery_request is the request code, can set its own +Startactivityforresult (Intent, to_gallery_request);

Open System camera, take photos and intercept

For pictures taken by the camera, first save the picture in the SD card and read it through the saved path and

1. Turn on the camera and take pictures

1File ImageFile =NewFile (File.getpath () + "/aviationboss");//get the folder path where the picture is saved2 3                         //If the folder does not exist, create4             if(!imagefile.exists ()) {5 imagefile.mkdirs ();6             }7 8                         //gets the picture path after the save, by default, the file name is the current time9Imageuri = Uri.fromfile (NewFile (ImageFile, characterparser.getdatestr_2 (System.currenttimemillis ()) + ". jpg"));Ten  One                         //open camera, set save path AIntent Intent =NewIntent (mediastore.action_image_capture); - Intent.putextra (Mediastore.extra_output, Imageuri); -                         //to_camera_request to open the camera's request code theStartactivityforresult (Intent, to_camera_request);

2. Get the picture after the photo, to intercept, first write a method

/** The method is to intercept the picture using the * parameter URI as the path of the picture *width and height for the frame width height of the *outputx and outputy for the picture of the width of the high *requestcode for the request code to intercept the picture*/Private voidTocropimage (Uri Uri,intWidthintHeightintOUTPUTX,intOutputy,intRequestcode) {Intent Intent=NewIntent ("Com.android.camera.action.CROP"); Intent.setdataandtype (URI,"image/*");//Set picture UriIntent.putextra ("Crop", "true"); Intent.putextra ("Aspectx", width); Intent.putextra ("Aspecty", height); Intent.putextra ("Outputx", OUTPUTX); Intent.putextra ("Outputy", outputy); Intent.putextra (Mediastore.extra_output, URI);//set the address where the image is saved after interception is completeIntent.putextra ("Return-data",false);//This is because the image is taken directly from the SD card, so there is no need to get the data through the Onactivityresult methodIntent.putextra ("OutputFormat", Bitmap.CompressFormat.JPEG.toString ()); Intent.putextra ("Nofacedetection",true);    Startactivityforresult (Intent, Requestcode); }

3. When the photo is completed, return to the activity's Onactivityresult method, where it is judged

1 @Override2     protected voidOnactivityresult (intRequestcode,intResultCode, Intent data) {3         //TODO auto-generated Method Stub4         Super. Onactivityresult (Requestcode, ResultCode, data);5         Switch(requestcode) {6          CaseTo_camera_request://results returned after taking a photo7             //call the Cropimage method to intercept a picture8 tocropimage (Imageuri, WIDTH, HEIGHT, output_x, output_y, to_crop_request);9              Break;Ten          CaseTo_gallery_request://The return value after capturing the picture and intercepting it through the system album One             if(data!=NULL){ ABitmap Bitmap =data.getparcelableextra ("data");//images obtained through the photo album, directly via data -             } -              Break; the          CaseTo_crop_request://return value After calling the Tocropimage method to intercept a picture -Bitmap Bitmap = Decodeuriasbitmap (Imageuri);//pictures captured by camera, obtained by image Uri -              Break; -         } +}

Now, there are two ways to get pictures.

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.