图像裁剪在Android 7.0及更高版本中无效

时间:2017-08-31 13:18:20

标签: android camera crop

我想从相机和裁剪中捕捉个人资料图片。在我们的代码中,相机图像正常工作,但裁剪无效。

相机方法

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() + "");
            }
        }
    }
}

2 个答案:

答案 0 :(得分:1)

Android does not have an image-cropping Intent

您应该添加a library to your project that handles image cropping,然后将该库与您startActivityForResult()电话中提供的图片一起使用。

答案 1 :(得分:0)

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库让事情变得更轻松。