In Android programming, selecting a photo album or taking a photo and then cutting it requires a lot
In the previous article, I only talked about how to get a photo from the album. Today I want to talk about how to cut the obtained photo.
Download complete source code
Let's talk about the idea first. The android system has its own image cutting application. Therefore, we only need to pass the photos we have obtained to the image cutting application, then, return the cut photo to our own interface and it will be OK.
Step 1: activate the system image or camera to obtain the photo and view the code.
/** Get from album */public void gallery (View view) {// activate the system image library, select an image Intent intent = new Intent (Intent. ACTION_PICK); intent. setType ("image/*"); // enable an Activity with a returned value. The request code is PHOTO_REQUEST_GALLERYstartActivityForResult (intent, PHOTO_REQUEST_GALLERY );} /** get from camera */public void camera (View view) {// activate camera Intent intent = new Intent ("android. media. action. IMAGE_CAPTURE "); // determines whether the memory card can be used and can be stored if (hasSdcard () {tempFile = new File (Environment. getExternalStorageDirectory (), PHOTO_FILE_NAME); // create uriUri uri = Uri from the file. fromFile (tempFile); intent. putExtra (MediaStore. EXTRA_OUTPUT, uri);} // start an Activity with a returned value. The request code is PHOTO_REQUEST_CAREMAstartActivityForResult (intent, PHOTO_REQUEST_CAREMA );}
Step 2: Use the startActivityForResult () method to obtain the corresponding return value in onActivityResult ().
@ Overrideprotected void onActivityResult (int requestCode, int resultCode, Intent data) {if (requestCode = PHOTO_REQUEST_GALLERY) {// if (data! = Null) {// obtain the full path Uri of the image = data. getData (); crop (uri) ;}} else if (requestCode = PHOTO_REQUEST_CAREMA) {// if (hasSdcard () {crop (Uri. fromFile (tempFile);} else {Toast. makeText (MainActivity. this, "no memory card found, unable to store photos! ", 0). show () ;}} else if (requestCode = PHOTO_REQUEST_CUT) {// if (data! = Null) {Bitmap bitmap = data. getParcelableExtra ("data"); this. iv_image.setImageBitmap (bitmap);} try {// Delete the temporary file tempFile. delete ();} catch (Exception e) {e. printStackTrace () ;}} super. onActivityResult (requestCode, resultCode, data );}
Step 3: notice that there is a crop () method in it. As the name suggests, it is used to cut the image.
/** Cut image */private void crop (Uri uri) {// crop image Intent intent Intent = new Intent ("com. android. camera. action. CROP "); intent. setDataAndType (uri, "image/*"); intent. putExtra ("crop", "true"); // ratio of the cropping frame, 1: 1intent. putExtra ("aspectX", 1); intent. putExtra ("aspectY", 1); // specifies the size (intent) of the output image after cropping. putExtra ("outputX", 250); intent. putExtra ("outputY", 250); intent. putExtra ("outputFormat", "JPEG"); // specifies the image format intent. putExtra ("noFaceDetection", true); // cancel face recognition intent. putExtra ("return-data", true); // enable an Activity with a returned value. The request code is PHOTO_REQUEST_CUTstartActivityForResult (intent, PHOTO_REQUEST_CUT );}
Step 4: After the image is cut, the image will be returned to us through onActivityResult ().
Through Bitmap bitmap = data. getParcelableExtra ("data"); we get the bitmap we want.
What do you want to do with this bitmap?
Since the above Code involves reading and writing sdcard, remember to authorize in the list file
Attached several