Skip to content Skip to sidebar Skip to footer

Cropactivity Is Not Starting In Onactivityresult Inside Fragment

I am calling an intent to select an image and later to crop the image into aspect ratio(1,1), but when i run the app the gallery is opening but when i select the image it closes an

Solution 1:

I have used this library in one of my projects and in that I didn't explicitly create a image picker but instead let the library handle it for me. To do this, in your code

mImageBtn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {


        CropImage.activity()
            .setAspectRatio(1, 1)
            .setMinCropWindowSize(500, 500)
            .start(getContext());
  }
});

Handle the result:

@OverrideprotectedvoidonActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) {
        CropImage.ActivityResultresult= CropImage.getActivityResult(data);

        if (resultCode == RESULT_OK) {

            UriimageUri= result.getUri();
            //handle the image



        } elseif (resultCode == CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE) {
            //handle error

        } elseif(resultCode == RESULT_CANCELED){
          //handle cancel case
        }
    }
}

For more information

Solution 2:

In case of using appcompact fragment we cannot use .start(getActivity()) to start the crop activity. Instead of this use:

.start(getContext(), this)

Here is code look like in fragments:

CropImage.activity(uri).setGuidelines(CropImageView.Guidelines.ON).start(getContext(), this);

Solution 3:

ActivityResult is more suitable :

@OverridepublicvoidonActivityResult(ActivityResult activityResult) {
      if (activityResult.getResultCode() == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) {
        CropImage.ActivityResultresult= CropImage.getActivityResult(activityResult.getData());
}
if (activityResult.getResultCode() == RESULT_OK) {
                            UriresultUri= result.getUri();
                        } elseif (activityResult.getResultCode() == CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE) {
                            Exceptionerror= result.getError();
                        }
                    }
                }

Post a Comment for "Cropactivity Is Not Starting In Onactivityresult Inside Fragment"