Objective
Android applications often encounter the need to upload photo albums, here is a record of how to choose and cut photo albums.
Album Selection picture
1. Activate the photo album or file Manager to get the photo with the following code:
private static final int take_picture_from_album = 1;
private void Takepicturefromalbum () {
Intent Intent = new Intent (intent.action_get_content);
Intent.settype ("image/*");
Startactivityforresult (Intent, take_picture_from_album);
}
2. Through the Startacitivityforresult () method, we can get the URI of the picture in Onacitivityresult (), and then pass the URI of the picture to the picture crop function, the code is as follows:
@Override protected void Onactivityresult (int requestcode, int resultcode, Intent data) {if (ResultCode = = Activit Y.RESULT_OK) {switch (requestcode) {case take_picture_from_album:if (data!= null) {Croppicturefrom
Album (Data.getdata ());
} break; Case PICTURE_CROP_CODE:IF (data!= null) {//This is the Bitmap of the cropped picture Bitmap Bitmap = Data.getparcelableextra ("Da
Ta ");
} break;
Default:break;
}} private static final int take_picture_from_album = 1;
private void Takepicturefromalbum () {Intent Intent = new Intent (intent.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 Intent ("Com.android.camera.action.CROP");
Intent.setdataandtype (Pictureuri, "image/*");
Intent.putextra ("Crop", "true"); Crop boxProportional Intent.putextra ("Aspectx", 1);
Intent.putextra ("Aspecty", 2);
After cropping, enter the size of the picture 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, Data.getparcelableextra ("data") can get the bitmap after the picture is cropped, then you can assign bitmap to the picture control.