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