1. Call the System camera
[Java]View plaincopyprint?
- // Photograph
- Public void takephotos (view ){
- Intent cameraintent = new intent (
- Android. provider. mediastore. action_image_capture );
- Startactivityforresult (cameraintent, camera_request );
- }
// Photography public void takephotos (view) {intent cameraintent = new intent (Android. provider. mediastore. action_image_capture); startactivityforresult (cameraintent, camera_request );}
[Java]View plaincopyprint?
- @ Override
- Protected void onactivityresult (INT requestcode, int resultcode, intent data) {// process it here
- // Todo auto-generated method stub
- If (requestcode = camera_request & resultcode = result_ OK & Data. getextras (). Get ("data ")! = NULL ){
- If (RSD. getphoto1url ()! = NULL) {// Delete the information of the previous image
- Deleteimgage (RSD. getphoto1url ());
- }
- Bitmap photo = (Bitmap) data. getextras (). Get ("data ");
- File basefile = filehelper. getpicbasefile ();
- If (basefile = NULL ){
- Showlongmessage ("SD card unavailable, please check the SD card situation ");
- Return;
- }
- String filename = getfilename (); // image name
- RSD. setphoto1url (filename );
- Savebitmap (photo, filename, basefile );
- Imgphoto. setimagebitmap (photo );
- }
- Super. onactivityresult (requestcode, resultcode, data );
- }
@ Overrideprotected void onactivityresult (INT requestcode, int resultcode, intent data) {// process it here // todo auto-generated method stubif (requestcode = camera_request & resultcode = result_ OK & data. getextras (). get ("data ")! = NULL) {If (RSD. getphoto1url ()! = NULL) {// Delete the information of the previous image deleteimgage (RSD. getphoto1url ();} bitmap photo = (Bitmap) data. getextras (). get ("data"); file basefile = filehelper. getpicbasefile (); If (basefile = NULL) {showlongmessage ("SD card unavailable, please check the SD card situation"); return;} string filename = getfilename (); // The RSD of the image name. setphoto1url (filename); savebitmap (photo, filename, basefile); imgphoto. setimagebitmap (photo);} super. onactivityresult (requestcode, resultcode, data );}
2.[Java]View plaincopyprint?
- // Get the file name
- Private string getfilename (){
- Simpledateformat SDF = new simpledateformat ("yyyymmddhhmmss", locale. getdefault ());
- Date crudate = calendar. getinstance (). gettime ();
- String strdate = SDF. Format (crudate );
- String username = config. getuser (question_record.this). getloginname ();
- String filename = username + strdate + ". jpg ";
- Return filename;
- }
// Get the file name private string getfilename () {simpledateformat SDF = new simpledateformat ("yyyymmddhhmmss", locale. getdefault (); Date crudate = calendar. getinstance (). gettime (); string strdate = SDF. format (crudate); string username = config. getuser (question_record.this ). getloginname (); string filename = username + strdate + ". jpg "; return filename ;}
3.[Java]View plaincopyprint?
- // Save the image
- Private void savebitmap (Bitmap bitmap, string filename, file basefile ){
- Fileoutputstream Bos = NULL;
- File imgfile = new file (basefile, "/" + filename );
- Try {
- Bos = new fileoutputstream (imgfile );
- Bitmap. Compress (bitmap. compressformat. JPEG, 100, Bos );
- } Catch (filenotfoundexception e ){
- // Todo auto-generated Catch Block
- E. printstacktrace ();
- } Finally {
- Try {
- Bos. Flush ();
- Bos. Close ();
- } Catch (ioexception e ){
- // Todo auto-generated Catch Block
- E. printstacktrace ();
- }
- }
- }
// Save the image private void savebitmap (Bitmap bitmap, string filename, file basefile) {fileoutputstream Bos = NULL; file imgfile = new file (basefile, "/" + filename ); try {Bos = new fileoutputstream (imgfile); bitmap. compress (bitmap. compressformat. JPEG, 100, Bos);} catch (filenotfoundexception e) {// todo auto-generated catch blocke. printstacktrace ();} finally {try {Bos. flush (); Bos. close ();} catch (ioexception e) {// todo auto-generated catch blocke. printstacktrace ();}}}
4. File help class
Public class filehelper {
- Private Static string filepath = "inspect/image ";
- /**
- * Basic image path
- * @ Return
- */
- Public static file getpicbasefile (){
- If (environment. getexternalstoragestate (). Equals (
- Environment. media_mounted) {// file available
- File F = new file (environment. getexternalstoragedirectory (), filepath );
- If (! F. exists () F. mkdirs ();
- Return F;
- } Else {
- Return NULL;
- }
- }
- }
Public class filehelper {Private Static string filepath = "inspect/image";/*** Image Base path * @ return */public static file getpicbasefile () {If (environment. getexternalstoragestate (). equals (environment. media_mounted) {// file available file F = new file (environment. getexternalstoragedirectory (), filepath); If (! F. exists () F. mkdirs (); Return F ;}else {return NULL ;}}}
5. The above code has been very detailed. If you have any questions about shoes you cannot understand, contact me.