Preface the Android application often encounter the need to upload photo album pictures, here to record how to make photo album image selection and cropping.
Photo Album Select picture 1. Activate the album or File Manager to get the photos, the code is as follows:
private static final int take_picture_from_album = 1;private void Takepicturefromalbum () {Intent Intent = new Intent (inten t.action_get_content); Intent.settype ("image/*"); Startactivityforresult (Intent, take_picture_from_album);}
2. With the Startacitivityforresult () method, we can get the URI of the picture in Onacitivityresult () and then pass the URI of the picture to the picture clipping function with the following code:
@Overrideprotected void Onactivityresult (int requestcode, int resultcode, Intent data) {if (ResultCode = = Activity.result _OK) {switch (requestcode) {case Take_picture_from_album:if (data! = null) {Croppicturefromalbum (Data.getdata ());} Break;case picture_crop_code:if (data = null) {//This is the bitmapbitmap of the cropped picture bitmap = Data.getparcelableextra ("Data");} Break;default:break;}}} private static final int take_picture_from_album = 1;private void Takepicturefromalbum () {Intent Intent = new Intent (inten t.action_get_content); Intent.settype ("image/*"); Startactivityforresult (Intent, take_picture_from_album);} private static final int picture_crop_code = 2;private void Croppicturefromalbum (Uri pictureuri) {Intent Intent = new Inte NT ("Com.android.camera.action.CROP"); Intent.setdataandtype (Pictureuri, "image/*"); Intent.putextra ("CROP", "true" )////Crop frame ratio intent.putextra ("Aspectx", 1), Intent.putextra ("Aspecty", 2);//The size of the input picture after cropping Intent.putextra ("Outputx", 300 ); Intent.putextra ("Outputy", 300);//Remove Face recognition Intent.puTExtra ("Nofacedetection", true); Intent.putextra ("Return-data", true); Startactivityforresult (Intent, PICTURE_CROP_ CODE);}
3. In Onactivityresult, the Data.getparcelableextra ("data") can be used to get the bitmap after the image is cropped, and then the bitmap can be assigned to the picture control.
Android pick album picture and crop