/** * Constructs an intent for image cropping. 调用图片剪辑程序 */ public static Intent createCropImageIntent(Uri photoUri) { Intent intent = new Intent("com.android.camera.action.CROP"); intent.setDataAndType(photoUri, "image/*"); // aspectX aspectY 是宽高的比例 // outputX outputY 是裁剪图片宽高 intent.putExtra("crop", "true"); intent.putExtra("aspectX", 1); intent.putExtra("aspectY", 1); intent.putExtra("outputX", 80); intent.putExtra("outputY", 80); intent.putExtra("return-data", true); return intent; }
调用方法:
private void cropPhoto(File file) { try { // 启动gallery去剪辑这个照片 final Intent intent = UIUtils.createCropImageIntent(Uri.fromFile(file)); startActivityForResult(intent, PHOTO_PICKED_WITH_DATA); } catch (Exception e) { // Toast.makeText(this, R.string.photoPickerNotFoundText, // Toast.LENGTH_LONG).show(); } }
调用相册图片进行裁剪:
// 封装请求Gallery的intent public static Intent createPhotoPickIntent() { Intent intent = new Intent(Intent.ACTION_GET_CONTENT, null); intent.setType("image/*"); intent.putExtra("crop", "true"); intent.putExtra("aspectX", 1); intent.putExtra("aspectY", 1); intent.putExtra("outputX", 80); intent.putExtra("outputY", 80); intent.putExtra("return-data", true); return intent; }
// 请求Gallery程序 protected void pickPhotoFromGallery() { try { // Launch picker to choose photo for selected contact final Intent intent = UIUtils.createPhotoPickIntent(); startActivityForResult(intent, PHOTO_PICKED_WITH_DATA); } catch (ActivityNotFoundException e) { // Toast.makeText(this, R.string.photoPickerNotFoundText1, // Toast.LENGTH_LONG).show(); } }
public void onActivityResult(int requestCode, int resultCode, Intent data) { if (resultCode != -1) return; switch (requestCode) { case PHOTO_PICKED_WITH_DATA: {// 调用Gallery返回的 final Bitmap image = data.getParcelableExtra("data"); addImage(image); break; } case CAMERA_WITH_DATA: {// 照相机程序返回的,再次调用图片剪辑程序去修剪图片 cropPhoto(mCurrentPhotoFile); break; } } }