Simple Android camera, display, and obtain the path before uploading

Source: Internet
Author: User

Simple Android camera, display, and obtain the path before uploading

Code in the Activity. I only paste the code of important events.

Public void doPhoto (View view) {destoryBimap (); String state = Environment. getExternalStorageState (); if (state. equals (Environment. MEDIA_MOUNTED) {Intent intent = new Intent ("android. media. action. IMAGE_CAPTURE "); startActivityForResult (intent, 1);} else {Toast. makeText (MainActivity. this, "No SD card", Toast. LENGTH_LONG ). show () ;}@ Override protected void onActivityResult (int requestCode, int resu LtCode, Intent data) {Uri uri = data. getData (); if (uri! = Null) {this. photo = BitmapFactory. decodeFile (uri. getPath ();} if (this. photo = null) {Bundle bundle = data. getExtras (); if (bundle! = Null) {this. photo = (Bitmap) bundle. get ("data");} else {Toast. makeText (MainActivity. this, "photo failed", Toast. LENGTH_LONG ). show (); return ;}} FileOutputStream fileOutputStream = null; try {// get the SD root directory String saveDir = Environment. getExternalStorageDirectory () + "/meitian_photos"; // create a directory File dir = new File (saveDir); if (! Dir. exists () dir. mkdir (); // generate the file name SimpleDateFormat t = new SimpleDateFormat ("yyyyMMddssSSS"); String filename = "MT" + (t. format (new Date () + ". jpg "; // new File file = new File (saveDir, filename); // open the file output stream fileOutputStream = new FileOutputStream (File); // generate the image file this. photo. compress (Bitmap. compressFormat. JPEG, 100, fileOutputStream); // The complete path of the photo. this. picPath = file. getPath (); ImageView image View = (ImageView) findViewById (R. id. showPhoto); imageView. setImageBitmap (this. photo);} catch (Exception e) {e. printStackTrace ();} finally {if (fileOutputStream! = Null) {try {fileOutputStream. close ();} catch (Exception e) {e. printStackTrace () ;}}}/ *** destroy image file */private void destoryBimap () {if (photo! = Null &&! Photo. isRecycled () {photo. recycle (); photo = null ;}}


Layout page

     
          
               
                
                
            
       
  
 

For the upload tool class, see this article:

Http://blog.csdn.net/zhouzme/article/details/18952053




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.