Call the Android system camera to take pictures and save pictures

Source: Internet
Author: User

Android calls system camera to take pictures and save pictures

To call the system camera:

// Add the following code to an event, not to say that the Click event is not written.
Startactivityforresult (new Intent (mediastore.action_image_capture),1);

Explanation: The parameter mediastore.action_image_capture is used for invoking the system camera, while the next 1 is the requested activity flag, which is used to process the returned result.

Can be understood as: Add a request flag to the activity of the system camera, and when processing the returned result with Onactivityresult, there is a processing scheme corresponding to this flag

Save Picture:

//method called when using Startactivityforresult to return results@Overrideprotected voidOnactivityresult (intRequestcode,intResultCode, Intent data) {        Super. Onactivityresult (Requestcode, ResultCode, data); //if the return value is normal,        if(ResultCode = =ACTIVITY.RESULT_OK) {            //Verify that the request code is one, that is, the second parameter of Startactivityforresult            Switch(requestcode) { Case1:savecameraimage (data);  Break; default:                 Break; }        }    }    /**Save picture of camera **/    Private voidsavecameraimage (Intent data) {//Check if SD card exists        if(!environment.getexternalstoragestate (). Equals (environment.media_mounted)) {log.i (TAG,"SD card isn't avaiable/writeable right now."); return; }        //name the picture.String name =NewDateFormat (). Format ("YYYYMMDD", Calendar.getinstance (Locale.china))+ ". jpg"; Bitmap BMP= (Bitmap) Data.getextras (). Get ("data");//parse the returned picture into bitmap//Save FileFileOutputStream fos =NULL; File File=NewFile ("/mnt/sdcard/test/"); File.mkdirs ();//Create a folderString fileName = "/mnt/sdcard/test/" + name;//Save Path        Try{//Write SD cardFOS =NewFileOutputStream (fileName); Bmp.compress (Bitmap.CompressFormat.JPEG,100, FOS); } Catch(FileNotFoundException e) {e.printstacktrace (); } finally {            Try{Fos.flush ();            Fos.close (); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); }        }//Show Pictures((ImageView) Findviewbyid (R.id.show_image)). Setimagebitmap (BMP); }


The most important thing is: Don't forget to add the permission OH

<uses-permission android:name= "Android.permission.CAMERA"/><!--Camera--
<uses-permission android:name= "Android.permission.WRITE_EXTERNAL_STORAGE"/><!--write SDcard-

Reference: http://blog.csdn.net/bill_ming/article/details/7730305

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.