Android takes pictures and saves them (compressing and splitting images ),
A small function module extracted from the project. If you need it, you can use it for reference.
:
Function: Save the photo to SDcard.
Specific requirements:
1. Save the image name in the format of: image name _ mobile phone number _random 4 bits .jpg
2. Break down, compress, and display images
3. Write the SDCard and save it
Specific implementation:
1. Define the layout File
2. Write Business Code
Here, we will only introduce how to call the camera to take photos, display images, and save images, and download the source code.
Click the button:
@ Override public void onClick (View v) {switch (v. getId () {case R. id. btn_getphoto: mPhotoName = mETphotoName. getEditableText (). toString (); mModel = mETmodel. getEditableText (). toString (); if (TextUtils. isEmpty (mPhotoName) | TextUtils. isEmpty (mModel) {Toast. makeText (this, "Enter the image name and model", 0 ). show (); return;} final String state = Environment. getExternalStorageState (); if (Environment. MEDIA_MOUNTED.equals (state) {final Intent takePictureImIntent = new Intent (MediaStore. ACTION_IMAGE_CAPTURE); ContentValues values = new ContentValues (); mPhotoUri = getContentResolver (). insert (MediaStore. images. media. EXTERNAL_CONTENT_URI, values); takePictureImIntent. putExtra (android. provider. mediaStore. EXTRA_OUTPUT, mPhotoUri); startActivityForResult (takePictureImIntent, REQUEST_CODE_TAKE_PICTURE);} else {Toast. makeText (this, "Check the memory card", 0 ). show () ;}break; default: break ;}}
OnActivityResult callback:
/* (Non-Javadoc) * @ see android. app. activity # onActivityResult (int, int, android. content. intent) */@ Override protected void onActivityResult (int requestCode, int resultCode, Intent data) {switch (requestCode) {case REQUEST_CODE_TAKE_PICTURE: // obtain the image if (resultCode = Activity. RESULT_ OK) {final Uri uri = mPhotoUri; Log. e ("MainActivity", uri + ""); if (uri! = Null) {processPicture (uri);} // you are prompted to save the image AlertDialog. builder builder = new AlertDialog. builder (this); builder. setTitle ("prompt"); builder. setMessage ("do you want to save the image? "); Builder. setNegativeButton ("cancel", null); builder. setPositiveButton ("yes", new DialogInterface. onClickListener () {@ Override public void onClick (DialogInterface dialog, int which) {String resetPhotoName = mPhotoName + "_" + mModel + "_" + Utils. getRandomString (4); Utils. savePhotoToSDCard (bitmap, Utils. genProjectPath (), resetPhotoName); Toast. makeText (MainActivity. this, "success --->" + Utils. genProjectPath () + resetPhotoName, 0 ). show (); // clear the mETphotoName text box. setText (""); mETmodel. setText (""); dialog. cancel () ;}}); builder. show ();} break; default: super. onActivityResult (requestCode, resultCode, data); break ;}}
Image Display:
/*** Image Display * @ param uri */private void processPicture (Uri uri) {final String [] projection = {MediaStore. images. media. DATA}; final Cursor cursor = managedQuery (uri, projection, null); cursor. moveToFirst (); final int columnIndex = cursor. getColumnIndexOrThrow (MediaStore. images. media. DATA); String imagePath = cursor. getString (columnIndex); Log. e ("MainActivity", imagePath); final String targetPath = BitmapUtils. toRegularHashCode (imagePath) + ". jpg "; BitmapUtils. compressBitmap (imagePath, targetPath, 640); // compress bitmap = BitmapUtils. decodeBitmap (imagePath, 150); // deploy mImage. setImageBitmap (bitmap); // display // upload server // final AvatarUrlUpdateRequest request = new AvatarUrlUpdateRequest (Region, mFileName, targetPath, mLoginResult ); // final LoginRequest request = new LoginRequest (0, "a@a.com", "123456"); // genericDataManager. retrieveData (request, this );}
Source code: http://download.csdn.net/detail/gao_chun/8128717