通过从图库

时间:2018-01-11 07:02:19

标签: android image android-intent crop onactivityresult

在我的应用程序中,有一项活动从相机拍摄照片,保存并在ImageView上显示。我使用 FilePath 类指定将其存储在路径上的位置,使用 CameraUtils 类来获取 file:URI 并将图像转换为 BitMap的。现在我的任务是添加裁剪功能,以摆脱不符合我们兴趣的区域。裁剪功能应在拍摄照片后立即在屏幕上激活,然后裁剪照片需要保存才能显示。在此之前,请在添加作物之前查看工作代码:

我有这种方法来捕捉图像:



    /*  Capture Image Method  */
        private void captureImage() {
            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);//Start intent with Action_Image_Capture
            fileUri = CameraUtils.getOutputMediaFileUri(this);//get fileUri from CameraUtils
            intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);//Send fileUri with intent
            //intent.putExtra("return-data",true); // Should be for crop
            startActivityForResult(intent, CAMERA_REQUEST_CODE);//start activity for result with CAMERA_REQUEST_CODE
        }




此方法显示捕获的图像



    /*  Show Captured over ImageView  */
        private void showCapturedImage() {
            if (!getImageUrl.equals("") && getImageUrl != null)
                imageView.setImageBitmap(CameraUtils.convertImagePathToBitmap(getImageUrl, false));
            else
                Toast.makeText(this, R.string.capture_image_failed, Toast.LENGTH_SHORT).show();
        }




裁剪图像的方法:



private void CropImage() {

        try {
            Intent CropIntent = new Intent("com.android.camera.action.CROP");
            CropIntent.setDataAndType(uri, "image/*");

            CropIntent.putExtra("crop", "true");
            CropIntent.putExtra("outputX", 180);
            CropIntent.putExtra("outputY", 180);
            CropIntent.putExtra("aspectX", 3);
            CropIntent.putExtra("aspectY", 4);
            CropIntent.putExtra("scaleUpIfNeeded", true);
            CropIntent.putExtra("return-data", true);

            startActivityForResult(CropIntent, 1003);
        } catch (ActivityNotFoundException ex) {
            //
        }
    }




onActivityResult()方法:



/**
 * public static final int PERMISSION_REQUEST_CODE = 1001;//request code for Camera and External Storage permission
 * private static final int CAMERA_REQUEST_CODE = 1002;//request code for capture image
 * private static final int CROP_REQUEST_CODE = 1003;//request code for crop
 */

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

            case CAMERA_REQUEST_CODE:
                try {
                    //When image is captured successfully
                    if (resultCode == RESULT_OK) {
                        //Check if device SDK is greater than 22 then we get the actual image path via below method
                        if (Build.VERSION.SDK_INT > 22)
                            getImageUrl = FilePath.getPath(MainActivity.this, fileUri);
                        else
                            //else we will get path directly
                            getImageUrl = fileUri.getPath();

                        //After image capture show captured image over image view
                        showCapturedImage();

                    } else
                        Toast.makeText(this, R.string.cancel_message, Toast.LENGTH_SHORT).show();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
        }
    }




直到这一点,everthing工作得很好,但是当插入裁剪功能时,我很难将if / / statement和所需的requestCodes添加到switch / case语句中,因为它会抛出未知错误。你能帮助我用多个requstCodes创建合适的逻辑并让它工作吗?

1 个答案:

答案 0 :(得分:1)

进行以下更改后,我可以使其正常工作。

首先,CropIntent通过密钥对值传递值,该值应该通过MediaStore.EXTRA_OUTPUT传递,并且uri没有使用正确的名称定义。

private void CropImage() {

        try {
            Intent CropIntent = new Intent("com.android.camera.action.CROP");
            CropIntent.setDataAndType(fileUri, "image/*");
            fileUri = CameraUtils.getOutputMediaFileUri(this);

            CropIntent.putExtra("crop", "true");
            CropIntent.putExtra("outputX", 180);
            CropIntent.putExtra("outputY", 180);
            CropIntent.putExtra("aspectX", 3);
            CropIntent.putExtra("aspectY", 4);
            CropIntent.putExtra("scaleUpIfNeeded", true);
            CropIntent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);

            startActivityForResult(CropIntent, CROP_REQUEST_CODE);

        } catch (ActivityNotFoundException ex) {
            //
        }
    }

其次,我在onActivityResult()

中用if / else替换了switch / case语句

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

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

        } else if (requestCode == CROP_REQUEST_CODE) {
                //Check if device SDK is greater than 22 then we get the actual image path via below method
                if (Build.VERSION.SDK_INT > 22)
                    getImageUrl = FilePath.getPath(MainActivity.this, fileUri);
                else
                    //else we will get path directly
                    getImageUrl = fileUri.getPath();
                //After image capture show captured image over image view
                showCapturedImage();
            }
        }