图像裁剪在Android 7.0及更高版本中不起作用
我想从相机捕获配置文件图像和裁剪。 在我们的代码相机图像正在工作,但裁剪不工作。
相机方法
private void cameraIntent(){
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (intent.resolveActivity(getActivity().getPackageManager()) != null) {
outputFileUri =
ProviderUtil.getOutputMediaFileUri(getActivity().getBaseContext());
intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
startActivityForResult(intent, REQUEST_CAMERA);
}
}
public void cropCapturedImage(){try {
getActivity().grantUriPermission("com.ht.msb.mysocialbuy.provider",outputFileUri,
Intent.FLAG_GRANT_WRITE_URI_PERMISSION |
Intent.FLAG_GRANT_READ_URI_PERMISSION);
Intent CropIntent = new Intent("com.android.camera.action.CROP");
CropIntent.setDataAndType(outputFileUri, "image/*");
CropIntent.putExtra("crop", "true");
if (imagebrowseType == 1) {
CropIntent.putExtra("outputX", 400);
CropIntent.putExtra("aspectX", 1);
CropIntent.putExtra("aspectY", 1);
} else {
CropIntent.putExtra("outputX", 600);
CropIntent.putExtra("aspectX", 6);
CropIntent.putExtra("aspectY", 4);
}
CropIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
CropIntent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
CropIntent.putExtra("outputY", 400);
CropIntent.putExtra("scaleUpIfNeeded", true);
CropIntent.putExtra("return-data", true);
CropIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
startActivityForResult(CropIntent, PIC_CROP);
} catch (ActivityNotFoundException e) {
Log.e("error_crop",e.toString()+" 1");
}
}
@Override
public void onActivityResult(int requestCode,int resultCode,Intent data){super.onActivityResult(requestCode,resultCode,data);
if (resultCode == Activity.RESULT_OK) {
if (requestCode == REQUEST_CAMERA) {
if (outputFileUri != null) {
cropCapturedImage();
}
} else if (requestCode == PIC_CROP) {
try {
Bitmap thePic;
if (data != null) {
thePic = MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(), outputFileUri);
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
thePic.compress(Bitmap.CompressFormat.JPEG, 90, bytes);
}
} catch (Exception e) {
Log.e("error", e.toString() + "");
}
}
}
Android没有图像裁剪Intent
。
您应该将库添加到处理图像裁剪的项目中,然后将该库与从startActivityForResult()
调用中返回的图像一起使用。
在onActvityResult方法中改变你的代码:
if (requestCode == PIC_CROP) {
if (data != null) {
// get the returned data
// get the cropped bitmap
if (data.getExtras()!=null) {
Bundle extras = data.getExtras();
Bitmap selectedBitmap = extras.getParcelable("data");
imgPhoto.setImageBitmap(selectedBitmap);
}
}
}
或者,使用uCrop库使事情变得更简单。
链接地址: http://www.djcxy.com/p/23933.html上一篇: Image cropping is not working in android 7.0 and higher version