Image cropping is often used in Android applications. Next we will set the photo in the album as the user's avatar to explain how to achieve cropping in Android.
<? XML version = "1.0" encoding = "UTF-8"?> <Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" Android: Orientation = "vertical" Android: layout_width = "fill_parent" Android: layout_height = "fill_parent"> <imageview Android: id = "@ + ID/imageid" Android: adjustviewbounds = "true" Android: src = "@ drawable/Shan" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content"/> <button Android: Id = "@ + ID/imageclipping" Android: layout_height = "50dip" Android: text = "cropping images" Android: layout_width = "150dip"/> </linearlayout>Package COM. test; import Java. io. bytearrayoutputstream; import Java. io. file; import Java. io. fileoutputstream; import Java. io. ioexception; import android. app. activity; import android. content. intent; import android. graphics. bitmap; import android. graphics. drawable. bitmapdrawable; import android. graphics. drawable. drawable; import android.net. uri; import android. OS. bundle; import android. OS. environment; import android Oid. provider. mediastore; import android. util. log; import android. view. view; import android. view. view. onclicklistener; import android. widget. button; import android. widget. imageview; import android. widget. toast; public class mainactivity extends activity {public static final int photozoom = 2; // scale public static final int photoresoult = 3; // result private string filename = environment. getexternalstoragedirecto Ry (). getabsolutepath () + "/temp.jpeg"; public static final string image_unspecified = "image/*"; imageview = NULL; button imageclippingbtn = NULL; @ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); imageview = (imageview) findviewbyid (R. id. imageid); imageclippingbtn = (button) findviewbyid (R. id. imageconping); imagecl Ippingbtn. setonclicklistener (New onclicklistener () {@ override public void onclick (view v) {drawable d = imageview. getdrawable (); Bitmap bitmap = (bitmapdrawable) d ). getbitmap (); savemybitmap (filename, bitmap); Uri uri = Uri. fromfile (new file (filename); startphotozoom (URI) ;}}) ;}@ override protected void onactivityresult (INT requestcode, int resultcode, intent data) {// processing result if (requestcode = phot Oresoult) {bundle extras = data. getextras (); If (extras! = NULL) {bitmap photo = extras. getparcelable ("data"); bytearrayoutputstream stream = new bytearrayoutputstream (); photo. compress (bitmap. compressformat. JPEG, 75, stream); // (0-100) compresses the imageview file. setimagebitmap (photo);} super. onactivityresult (requestcode, resultcode, data);} // image cropping method public void startphotozoom (URI) {intent = new intent ("com. android. camera. action. crop "); intent. setdataandtype (Uri, image_unspecified); intent. putextra ("crop", "true"); // aspectx aspecty is a width-high ratio intent. putextra ("aspectx", 1); intent. putextra ("aspecty", 1); // outputx outputy is the width and height intent of the cropped image. putextra ("outputx", 64); intent. putextra ("outputy", 64); intent. putextra ("Return-Data", true); startactivityforresult (intent, photoresoult);} // save bitmap as the public void savemybitmap (string filename, bitmap mbitmap) {file F = NULL; fileoutputstream fout = NULL; try {log. E ("mainactivity", "filename:" + filename); F = new file (filename); F. createnewfile (); fout = new fileoutputstream (f); mbitmap. compress (bitmap. compressformat. JPEG, 100, fout);} catch (exception e) {e. printstacktrace ();} Try {fout. flush ();} catch (exception e) {e. printstacktrace ();} Try {fout. close ();} catch (ioexception e) {e. printstacktrace ();}}