【问题标题】:Image cropping is not working in android 7.0 and higher version图像裁剪在 android 7.0 及更高版本中不起作用
【发布时间】:2023-03-13 14:06:01
【问题描述】:

我想从相机中捕获个人资料图像并进行裁剪。在我们的代码中,相机图像可以正常工作,但 cropping 不工作。

相机方法

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 camera crop


【解决方案1】:

Android does not have an image-cropping Intent.

您应该添加a library to your project that handles image cropping,然后将该库与您从startActivityForResult() 调用返回的图像一起使用。

【讨论】:

  • 是否有任何官方文件表明该牛轧糖没有作物意图行动?
  • @LovekushVishwakarma:你问错问题了。您应该尝试确定是否任何与作物相关的Intent 操作的官方文档。如果你search the Android documentation for crop,你会很快确定没有裁剪Intent动作。
【解决方案2】:

在 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 库让事情变得更简单。

【讨论】:

    猜你喜欢
    • 2017-06-23
    • 1970-01-01
    • 1970-01-01
    • 2017-07-17
    • 1970-01-01
    • 2013-05-02
    • 1970-01-01
    • 2018-06-03
    • 1970-01-01
    相关资源
    最近更新 更多