Android calls the System camera and keeps the image local.

Source: Internet
Author: User

1. Call the System camera

 

[Java]View plaincopyprint?
  1. // Photograph
  2. Public void takephotos (view ){
  3. Intent cameraintent = new intent (
  4. Android. provider. mediastore. action_image_capture );
  5. Startactivityforresult (cameraintent, camera_request );
  6. }
// Photography public void takephotos (view) {intent cameraintent = new intent (Android. provider. mediastore. action_image_capture); startactivityforresult (cameraintent, camera_request );}
[Java]View plaincopyprint?
  1. @ Override
  2. Protected void onactivityresult (INT requestcode, int resultcode, intent data) {// process it here
  3. // Todo auto-generated method stub
  4. If (requestcode = camera_request & resultcode = result_ OK & Data. getextras (). Get ("data ")! = NULL ){
  5. If (RSD. getphoto1url ()! = NULL) {// Delete the information of the previous image
  6. Deleteimgage (RSD. getphoto1url ());
  7. }
  8. Bitmap photo = (Bitmap) data. getextras (). Get ("data ");
  9. File basefile = filehelper. getpicbasefile ();
  10. If (basefile = NULL ){
  11. Showlongmessage ("SD card unavailable, please check the SD card situation ");
  12. Return;
  13. }
  14. String filename = getfilename (); // image name
  15. RSD. setphoto1url (filename );
  16. Savebitmap (photo, filename, basefile );
  17. Imgphoto. setimagebitmap (photo );
  18. }
  19. Super. onactivityresult (requestcode, resultcode, data );
  20. }
@ 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?

  1. // Get the file name
  2. Private string getfilename (){
  3. Simpledateformat SDF = new simpledateformat ("yyyymmddhhmmss", locale. getdefault ());
  4. Date crudate = calendar. getinstance (). gettime ();
  5. String strdate = SDF. Format (crudate );
  6. String username = config. getuser (question_record.this). getloginname ();
  7. String filename = username + strdate + ". jpg ";
  8. Return filename;
  9. }
// 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?

  1. // Save the image
  2. Private void savebitmap (Bitmap bitmap, string filename, file basefile ){
  3. Fileoutputstream Bos = NULL;
  4. File imgfile = new file (basefile, "/" + filename );
  5. Try {
  6. Bos = new fileoutputstream (imgfile );
  7. Bitmap. Compress (bitmap. compressformat. JPEG, 100, Bos );
  8. } Catch (filenotfoundexception e ){
  9. // Todo auto-generated Catch Block
  10. E. printstacktrace ();
  11. } Finally {
  12. Try {
  13. Bos. Flush ();
  14. Bos. Close ();
  15. } Catch (ioexception e ){
  16. // Todo auto-generated Catch Block
  17. E. printstacktrace ();
  18. }
  19. }
  20. }
// 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 {
  1. Private Static string filepath = "inspect/image ";
  2. /**
  3. * Basic image path
  4. * @ Return
  5. */
  6. Public static file getpicbasefile (){
  7. If (environment. getexternalstoragestate (). Equals (
  8. Environment. media_mounted) {// file available
  9. File F = new file (environment. getexternalstoragedirectory (), filepath );
  10. If (! F. exists () F. mkdirs ();
  11. Return F;
  12. } Else {
  13. Return NULL;
  14. }
  15. }
  16. }
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.

Related Article

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.