解决办法
可以定义一个Uri对象来保存剪切后的图片
File file = new File(Environment.getExternalStorageDirectory(),BitmapUtil.generateFileName());
corpUri = Uri.fromFile(file);
打开相机:
/**
* 打开照相机
*/
private void openCamera() {
String SDState = Environment.getExternalStorageState();
if(SDState.equals(Environment.MEDIA_MOUNTED)){
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);// action is
ContentValues values = new ContentValues();
imageUri = this.getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, imageUri);
startActivityForResult(intent, REQUEST_CODE_CAMARE);
}else{
ToastUtil.showShort(mActivity, "SD卡不存在");
}
}
打开相册:
/**
* 打开相册
*/
private void openPhoto() {
Intent intent = new Intent(Intent.ACTION_PICK, null);
intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,"image/*");
startActivityForResult(intent, REQUEST_CODE_PHOTO);
}
调用系统裁剪:
private void cropImageUri() {
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(imageUri, "image/*");
// 下面这个crop=true是设置在开启的Intent中设置显示的VIEW可裁剪
intent.putExtra("crop", "true");
intent.putExtra("scale", true);// 去黑边
intent.putExtra("scaleUpIfNeeded", true);// 去黑边
// aspectX aspectY 是宽高的比例
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
intent.putExtra("outputX", imageSize);
intent.putExtra("outputY", imageSize);
intent.putExtra("return-data", false);设置为不返回数据
intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
intent.putExtra("noFaceDetection", true);
intent.putExtra(MediaStore.EXTRA_OUTPUT, corpUri);
intent.putExtra("scale", true);
startActivityForResult(intent, REQUEST_CODE_CROP);
}
在很多博客中都把“return-data”设置为了true然后在onActivityResult中通过data.getParcelableExtra("data")来获取数据,不过这样的话imageSize这个变量的值就不能太大了,不然你的程序就挂了。这里也就是我遇到问题的地方了,在大多数高配手机上这样用是没有问题的,不过很多低配手机就有点hold不住了,直接就异常了,包括我们的国产神机米3也没能hold住,所以我建议大家不要通过return data 大数据,小数据还是没有问题的,说以我们在剪切图片的时候就尽量使用Uri这个东东来帮助我们。
下面是我们进行剪裁用到的一些参数
Exta Options Table for image/* crop:
SetExtra | DataType | Description |
crop | String | Signals the crop feature |
aspectX | int | Aspect Ratio |
aspectY | int | Aspect Ratio |
outputX | int | width of output created from this Intent |
outputY | int | width of output created from this Intent |
scale | boolean | should it scale |
return-data | boolean | Return the bitmap with Action=inline-data by using the data |
data | Parcelable | Bitmap to process, you may provide it a bitmap (not tested) |
circleCrop | String | if this string is not null, it will provide some circular cr |
MediaStore.EXTRA_OUTPUT ("output") | URI | Set this URi to a File:///, see example code |
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case REQUEST_CODE_CROP:
changeHead();
break;
case REQUEST_CODE_PHOTO:
if (data != null) {
imageUri= data.getData();
cropImageUri();
}
break;
case REQUEST_CODE_CAMARE:
cropImageUri();
break;
default:
break;
}
}