【问题标题】:Receive two requestCode == UCrop.REQUEST_CROP in onActivityResult?在 onActivityResult 中收到两个 requestCode == UCrop.REQUEST_CROP?
【发布时间】:2018-01-12 17:35:41
【问题描述】:

我正在使用uCrop 库来执行图像裁剪。

我需要在同一个活动中剪切两个不同的图像。但是使用我的代码,返回会陷入一个循环。

我怎样才能把这个回报分开,独立对待?

代码:

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (resultCode == RESULT_OK) {

            if (requestCode == UCrop.RESULT_ERROR) {
                Toast.makeText(this, "uCrop error", Toast.LENGTH_SHORT).show();
                return;
            }

            if (requestCode == UCrop.REQUEST_CROP) {

                mSelectImage.setImageResource(0);
                final Uri imgUri = UCrop.getOutput(data);
                mResultUri = imgUri;
                mSelectImage.setImageURI(mResultUri);
                temDadosSalvar = true;

                Toast.makeText(this, "Cortada", Toast.LENGTH_SHORT).show();
                return;
            }


            if (requestCode == CAMERA_REQUEST_CODE && resultCode == RESULT_OK) {

                mImageUri = data.getData();
                if (mImageUri == null) {
                    mProgress.dismiss();
                    return;
                } else {
                    File tempCropped = new File(getCacheDir(), "tempImgCropped.png");
                    Uri destinationUri = Uri.fromFile(tempCropped);
                    UCrop uCrop = UCrop.of(mImageUri, destinationUri);

                    uCrop = advancedConfig(uCrop);
                    uCrop.start(this);
                }
            }

            if (requestCode == CAMERA_REQUEST_CODE_CAPA && resultCode == RESULT_OK) {


                mImageUri = data.getData();
                if (mImageUri == null) {
                    mProgress.dismiss();
                    return;
                } else {
                    File tempCropped = new File(getCacheDir(), "tempImgCropped.png");
                    Uri destinationUri = Uri.fromFile(tempCropped);
                    UCrop uCrop = UCrop.of(mImageUri, destinationUri);


                    uCrop = advancedConfig(uCrop);
                    uCrop.start(this);
                }
            }
        }
    }

【问题讨论】:

    标签: android ucrop


    【解决方案1】:

    您可以创建两个类变量来识别正在裁剪的图像。

    private Uri mDestinationCapa;
    private Uri mDestinationCamera;
    
     if (requestCode == CAMERA_REQUEST_CODE && resultCode == RESULT_OK) {
                mImageUri = data.getData();
                if (mImageUri == null) {
                  ...
                } else {
                    File tempCropped = new File(getCacheDir(), "tempImgCropped.png");
                    mDestinationCamera= Uri.fromFile(tempCropped);
                    ...
                }
            }
    
            if (requestCode == CAMERA_REQUEST_CODE_CAPA && resultCode == RESULT_OK) {
                mImageUri = data.getData();
                if (mImageUri == null) {
                 ...
                } else {
                    File tempCropped = new File(getCacheDir(), "tempImgCropped.png");
                    mDestinationCapa = Uri.fromFile(tempCropped);
                    ...
                }
            }
    

    在 ActivityResult 上,您可以将结果 Uri 与本地 Uris 进行比较:

    if (requestCode == UCrop.REQUEST_CROP) {
    
            final Uri imgUri = UCrop.getOutput(data);
            if(mDestinationCamera != null && imgUri.equals(mDestinationCamera)){
                //save camera cropped image 
    
            }if(mDestinationCamera != null && imgUri.equals(mDestinationCamera)){
                //save capa cropped image
            }
            return;
        }
    

    【讨论】:

    • 好的。谢谢!!我会尽力!! @dledo
    猜你喜欢
    • 2012-05-20
    • 1970-01-01
    • 2021-05-27
    • 1970-01-01
    • 2015-01-29
    • 2019-08-13
    • 2016-01-27
    • 2012-06-07
    • 2015-09-19
    相关资源
    最近更新 更多