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