This article uses the Android native selection and cropping feature. Here's the code:
public class Pickandcropactivity extends appcompatactivity implements view.onclicklistener{public static final int REQ uest_code_pick_img = 1; public static final int request_code_crop_img = 2; /** * ImageView Show selected pictures and cropped images */Private ImageView img; /** * Select the resulting picture URI */private URI MUri; @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_pick_and_crop); img = (ImageView) Findviewbyid (r.id.img); } @Override public void OnClick (View v) {switch (V.getid ()) {r.id.pick: Startactivityforresult (Intentutils.generatepickimgintent (), request_code_pick_img); Break Case R.id.crop:startactivityforresult (Intentutils.generatecropimgintent (MUri, Max), request_code_crop_img) ; Break }} @Override protected void Onactivityresult (iNT Requestcode, int resultcode, Intent data) {switch (requestcode) {case Request_code_pick_i Mg:if (ResultCode! = RESULT_OK | | data = NULL) break; MUri = Data.getdata (); Contentresolver CR = This.getcontentresolver (); try {Bitmap Bitmap = Bitmapfactory.decodestream (Cr.openinputstream (MUri)); Img.setimagebitmap (bitmap); } catch (FileNotFoundException e) {e.printstacktrace (); } break; Case Request_code_crop_img:if (ResultCode! = RESULT_OK | | data = NULL) break; Bitmap Bmap = Data.getparcelableextra ("Data"); Img.setimagebitmap (BMAP); Break } super.onactivityresult (Requestcode, ResultCode, data); }}
The Intentutils class used in the middle:
public class intentutils{ /** * Constructs a select picture of the Intent * * @return */public static Intent Generatepickimgintent () { Intent Intent = new Intent (Intent.action_pick); Intent.settype ("image/*"); return intent; } /** * Constructs a cropped picture of intent * * @param uri * @param size * @return */public static Intent generatecropimgintent (URI uri, int size) { Intent Intent = new Intent ("Com.android.camera.action.CROP "); Muri is a picture URI that has been selected Intent.setdataandtype (URI, "image/*"); Intent.putextra ("Crop", "true"); Cropping frame proportional intent.putextra ("Aspectx", 1); Intent.putextra ("Aspecty", 1); Output picture size Intent.putextra ("Outputx", size); Intent.putextra ("Outputy", size); Intent.putextra ("Return-data", true); return intent;} }
Android Select local picture and crop