Image cropping is not working in android 7.0 and higher version

i want to capture profile image from camera and crop. in our code camera image is working but cropping is not working.

camera method

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 does not have an image-cropping Intent .

You should add a library to your project that handles image cropping, then use that library with the image that you get back from your startActivityForResult() call.


in onActvityResult method change your code like:

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);

            }

        }
    }

Or, Use uCrop library which makes things easier.

链接地址: http://www.djcxy.com/p/23934.html

上一篇: key()'或'in'?

下一篇: 图像裁剪在Android 7.0及更高版本中不起作用