Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to crop an image in android

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 406
    Comment on it

    Step1: First we have to add jcenter() repository to Project build.gradle

    allprojects {
    	    repositories {
    
    		jcenter()
    	    }
    	}

    Step2: Now add cropping library in our app build.gradle and synchronize the application.

        compile 'com.theartofdev.edmodo:android-image-cropper:2.2.+'

    Step3: In our manifest file adding CropImageActivity under application tag

        <activity android:name="com.theartofdev.edmodo.cropper.CropImageActivity"/>

    Step4: Initialize the onClick method to begin cropping and to start CropImageActivity

    tvCropImage.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {
    
                v.startAnimation(AnimationUtils.loadAnimation(getActivity(), android.R.anim.fade_in));
                if (adapter.isAnySelected()) {
                    File imgFile = new File(adapter.getPath());
                    Intent intent = CropImage.activity(Uri.fromFile(imgFile))
                           .setGuidelines(CropImageView.Guidelines.ON)
                           .setAspectRatio(4,3)
                           .setFixAspectRatio(true)
                           .setCropShape(CropImageView.CropShape.RECTANGLE)
                           .getIntent(getContext());
    
                    startActivityForResult(intent, CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE);
                } else {
    
                    Toast.makeText(getActivity(), "Please select images to crop.", Toast.LENGTH_SHORT).show();
                }
           }
    });

    Step5: To get the cropping result in our activity we have to override onActivityResult() method

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
    
            if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) {
    
                CropImage.ActivityResult result = CropImage.getActivityResult(data);
                if (resultCode == Activity.RESULT_OK) {
    
                    Uri resultUri = result.getUri();
                } else if (resultCode == CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE) {
    
                    Exception error = result.getError();
    		Log.v("Exception error:",error.toString);
                }
            }
    }

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: