Crop images in photos and albums. Crop images in photos and albums.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <Button android:id="@+id/take_photo" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Take Photo" /> <Button android:id="@+id/get_photo" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="get Photo" /> <ImageView android:id="@+id/picture" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" /></LinearLayout>
Package com. example. choosepictest; import java. io. file; import java. io. IOException; import android. app. activity; import android. content. intent; import android. graphics. bitmap; import android.net. uri; import android. OS. bundle; import android. OS. environment; import android. provider. mediaStore; import android. view. view; import android. view. view. onClickListener; import android. widget. button; import android. widget. I MageView; public class MainActivity extends Activity implements OnClickListener {public static final int TAKE_PHOTO = 1; public static final int CROP_PHOTO = 2; public static final int GET_PHOTO = 3; private Button takePhoto; private Button getPhoto; private ImageView picture; private Uri headImgUri; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState ); SetContentView (R. layout. activity_main); takePhoto = (Button) findViewById (R. id. take_photo); getPhoto = (Button) findViewById (R. id. get_photo); picture = (ImageView) findViewById (R. id. picture); takePhoto. setOnClickListener (this); getPhoto. setOnClickListener (this) ;}@ Override public void onClick (View v) {switch (v. getId () {case R. id. take_photo: takePhoto (); break; case R. id. get_photo: getPhoto (); Break; default: break; }}// take a photo of private void takePhoto () {File appDir = new File (Environment. getExternalStorageDirectory (), "/etoury/picCache"); if (! AppDir. exists () {appDir. mkdir ();} String fileName = "user_head" + ". jpg "; File outputImage = new File (appDir, fileName); try {if (outputImage. exists () {outputImage. delete ();} outputImage. createNewFile ();} catch (IOException e) {e. printStackTrace ();} headImgUri = Uri. fromFile (outputImage); Intent intent = new Intent ("android. media. action. IMAGE_CAPTURE "); intent. putExtra (MediaStore. EXTRA_OUTPUT, headImgUri); startActivityForResult (intent, TAKE_PHOTO);} // The private void getPhoto () {Intent intent Intent = new Intent (intent. ACTION_PICK, android. provider. mediaStore. images. media. EXTERNAL_CONTENT_URI); startActivityForResult (intent, GET_PHOTO);}/*** crop */private void crop (Uri uri) {// crop image Intent intent Intent = new intent ("com. android. camera. action. CROP "); intent. setDataAndType (uri, "image/*"); // The crop = true is set to crop the display VIEW in the enabled Intent. putExtra ("crop", "true"); intent. putExtra ("scale", true); // proportion of the cropped box to the Black edge, intent. putExtra ("aspectX", 1); // The output is a proportional intent in the X direction. putExtra ("aspectY", 1); // the size of the output image after cropping. It cannot be too large. 500 program crashes intent. putExtra ("outputX", 256); intent. putExtra ("outputY", 256); // image format/* intent. putExtra ("outputFormat", "JPEG"); */intent. putExtra ("outputFormat", Bitmap. compressFormat. JPEG. toString (); // intent. putExtra ("noFaceDetection", true); // cancel face recognition intent. putExtra ("return-data", true); // true: uri is returned; false: uri is not returned. // The cropped image at the same address overwrites the image intent. putExtra (MediaStore. EXTRA_OUTPUT, headImgUri); startActivityForResult (intent, CROP_PHOTO);} @ Override protected void onActivityResult (int requestCode, int resultCode, Intent data) {switch (requestCode) {case GET_PHOTO: if (resultCode = RESULT_ OK) {crop (data. getData ();} break; case TAKE_PHOTO: if (resultCode = RESULT_ OK) {crop (headImgUri);} break; case CROP_PHOTO: if (resultCode = RESULT_ OK) {Bitmap cropbitmap = data. getParcelableExtra ("data"); picture. setImageBitmap (cropbitmap);} break; default: break ;}}}
Summary:
1. Take a photo and return an image of full size.
2. The address of the image returned from the photo is a file in a directory.
3. The cropped image address covers the address of the full-size image.
4. The album intent returns a uir instead of a string.
5. The cropped image cannot overwrite the uri returned by the album (please note)