The system already has things, if we do not have new requirements, direct call is the most direct. Here's how to call the system camera to take pictures and save pictures and how to call system albums.
First look at the core method of invoking the system camera:
New Intent (mediastore.action_image_capture); Startactivityforresult (camera, camera);
The data returned by the camera is obtained through the following callback method and processed:
public static final int CAMERA = 0x01;
@Overrideprotected voidOnactivityresult (intRequestcode,intResultCode, Intent data) { Super. Onactivityresult (Requestcode, ResultCode, data); if(Requestcode = = CAMERA && ResultCode = = Activity.result_ok &&NULL!=data) {String sdstate=environment.getexternalstoragestate (); if(!sdstate.equals (environment.media_mounted)) {GameLog.log (Tag,"SD card Unmount"); return; } NewDateFormat (); String name= Dateformat.format ("Yyyymmdd_hhmmss", Calendar.getinstance (Locale.china)) + ". jpg"; Bundle Bundle=Data.getextras (); //get the data returned by the camera and convert it to a picture formatBitmap Bitmap = (Bitmap) bundle.get ("Data")); FileOutputStream Fout=NULL; File File=NewFile ("/sdcard/pintu/"); File.mkdirs (); String filename=file.getpath () +name; Try{fout=Newfileoutputstream (filename); Bitmap.compress (Bitmap.CompressFormat.JPEG,100, Fout); } Catch(FileNotFoundException e) {e.printstacktrace (); }finally{ Try{Fout.flush (); Fout.close (); } Catch(IOException e) {e.printstacktrace (); } } //Show Pictures }}
Here's how to call a system album and get a Photo:
- Intent picture = new Intent (Intent.action_pick,android.provider.mediastore.images.media.external_content_uri);
- Startactivityforresult (picture, picture);
The following is the appropriate callback method:
@Overrideprotected voidOnactivityresult (intRequestcode,intResultCode, Intent data) { Super. Onactivityresult (Requestcode, ResultCode, data); if(Requestcode = = CAMERA && ResultCode = = Activity.result_ok &&NULL!=data) {Uri SelectedImage=Data.getdata (); String[] Filepathcolumns={MediaStore.Images.Media.DATA}; Cursor C= This. Getcontentresolver (). Query (SelectedImage, Filepathcolumns,NULL,NULL,NULL); C.movetofirst (); intColumnIndex = C.getcolumnindex (filepathcolumns[0]); String PicturePath=c.getstring (columnindex); C.close (); //get the picture and show }
This completes the system call, very simple.
How Android calls system cameras to save and call system albums