Search code examples
javaandroidimagecrop

Crop image in a fragment


I'm using this library for cropping images https://android-arsenal.com/details/1/3487 I have a problem when trying to crop an image from a fragment and not from a regular activity and it doesn't work, even though I do the exact thing, this is my onActivityResult:

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == getActivity().RESULT_OK) {
        if (requestCode == SELECT_PICTURE) {



            CropImage.activity(data.getData())
                    .setCropShape(CropImageView.CropShape.OVAL)
                    .setGuidelines(CropImageView.Guidelines.OFF)
                    .setAutoZoomEnabled(false)
                    .start(getContext(), FeedFragment.this);

        }

        if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) {
            CropImage.ActivityResult result = CropImage.getActivityResult(data);
            try {
                Uri resultUri = result.getUri();
                InputStream is = getActivity().getContentResolver().openInputStream(resultUri);
                Bitmap photoBitmap = BitmapFactory.decodeStream(is);
                imgButton.setImageBitmap(photoBitmap);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        }
    }
}

and the problem is in this line:

  .start(getContext(), FeedFragment.this);

the error:

Failure delivering result ResultInfo{who=null, request=65537, result=-1, data=Intent { dat=content://media/external/images/media/5560 flg=0x1 (has extras) }} to activity {apps.berant.knowu/apps.berant.knowu.MainActivity}: android.content.ActivityNotFoundException: Unable to find explicit activity class {apps.berant.knowu/com.theartofdev.edmodo.cropper.CropImageActivity}; have you declared this activity in your AndroidManifest.xml?

thanks for the help :)


Solution

  • add line AndroidManifest.xml

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