Crop image in a fragment

crop image in android example
auto crop image android
custom crop image in android example github
crop image from camera and gallery in android github
android crop image like whatsapp
cropimage
android-image cropper
onactivityresult in 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 :)

add line AndroidManifest.xml

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

one of the tabbed Fragments. I start the camera/gallery activity with CropImage.​startPickImageActivity(getActivity()) from within the Fragment. The crop image function is, as the name suggests, reserved for photos and image files only. This means it will only work correctly if you upload an image to crop. Other files like documents won’t work. If you are satisfied with cropping the image, you can save it in a number of different formats. They are listed below.

As it's mentioned in docs, I just simply added this to manifest:

<activity
        android:name="com.yalantis.ucrop.UCropActivity"
        android:screenOrientation="portrait"
        android:theme="@style/Theme.AppCompat.Light.NoActionBar"/>

start(this); // for fragment (DO NOT use `getActivity()`) CropImage.activity() .start(​getContext(), this);. Override onActivityResult method in your activity to get crop  There is no feature to restrict the image size, it may be problematic as it depends on the image format used. But you can restrict the resolution of the resulted cropped image using required size feature. Setting it to 1080 x 1920 and using jpeg format with 90-95 quality should limit the potential size under 2MB.

<android.support.v7.widget.AppCompatImageView
    android:id="@+id/iv_house"
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:scaleType="fitXY"/> 

In Fragment

@Override
public void onClick(View v) {
    switch (v.getId()) {
    case R.id.iv_house:
    if (CropImage.isExplicitCameraPermissionRequired(getActivity())) {
        requestPermissions(new String[]{Manifest.permission.CAMERA}, CropImage.CAMERA_CAPTURE_PERMISSIONS_REQUEST_CODE);
    } else {
        CropImage.startPickImageActivity(getActivity());
    }
    break;
}

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

    if (requestCode == CropImage.PICK_IMAGE_CHOOSER_REQUEST_CODE) {
        Uri imageUri = CropImage.getPickImageResultUri(getActivity(), data);
        if (CropImage.isReadExternalStoragePermissionsRequired(getActivity(), imageUri)) {
            mCropImageUri = imageUri;
            requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, CropImage.PICK_IMAGE_PERMISSIONS_REQUEST_CODE);    
        } else {
            Intent intent = CropImage.activity(imageUri)
                .setGuidelines(CropImageView.Guidelines.ON)
                .setFixAspectRatio(true)
                .setAspectRatio(2,1)
                .getIntent(getActivity());
            getActivity().startActivityForResult(intent, CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE);
        }
    }

    if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) {
        CropImage.ActivityResult result = CropImage.getActivityResult(data);
        if (resultCode == RESULT_OK) {
            Uri resultUri = result.getUri();
            selectedFilePath = resultUri.getPath();
            imageView.setImageURI(resultUri);
        } else if (resultCode == CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE) {
            Exception error = result.getError();
            Toast.makeText(getActivity(), error.getMessage(), Toast.LENGTH_LONG).show();
        }
    }
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    //super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    if (requestCode == CropImage.CAMERA_CAPTURE_PERMISSIONS_REQUEST_CODE) {
        if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            CropImage.startPickImageActivity(getActivity());
        } else {
            Toast.makeText(getActivity(), "Cancelling, required permissions are not granted", Toast.LENGTH_LONG).show();
        }
    }

    if (requestCode == CropImage.PICK_IMAGE_PERMISSIONS_REQUEST_CODE) {
        if (mCropImageUri != null && grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            Intent intent = CropImage.activity(mCropImageUri)
                .setGuidelines(CropImageView.Guidelines.ON)
                .setFixAspectRatio(true)
                .setAspectRatio(2,1)
                .getIntent(getActivity());
            getActivity().startActivityForResult(intent, CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE);
        } else {
            Toast.makeText(getActivity(), "Cancelling, required permissions are not granted", Toast.LENGTH_LONG).show();
        }
    }
}

In Activity

private Uri mCropImageUri;  

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    //super.onActivityResult(requestCode, resultCode, data);
    Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.container);
    fragment.onActivityResult(requestCode, resultCode, data);
}

In AndroidManifest.xml

<activity
    android:name="com.theartofdev.edmodo.cropper.CropImageActivity"
    android:theme="@style/Base.Theme.AppCompat" />

CropImage$ActivityBuilder (Showing top 20 results out of 315) @param fragment fragment to receive result */ public void start( @NonNull Context context​,  you started the activity using .start(getActivity()); this way the result will only return in the activity, not in the fragment. If you use appcompat fragment (recomended) you can call .start(getContext(), this) otherwise you need to call .getIntent(getContext()) and then use this intent to start the activity from your fragment. Thank you.

start(this); // for fragment (DO NOT use `getActivity()`) CropImage.activity() .start(​getContext(), this);. Override onActivityResult method in your activity to get crop  Resize the Crop Image tool. Select any of the resize handles on the crop rectangle. The pointer changes to a double-headed arrow . Click and drag the mouse to resize the crop rectangle. Move the Crop Image tool. Move the pointer inside the boundary of the crop rectangle. The pointer changes to a fleur shape . Click and drag the mouse to move the rectangle over the image.

Project: Android-Demo_ImageCroper File: MainFragment.java Source Code and getBitmap(); } startActivity(intent); } else { Log.e("AIC", "Failed to crop image",  Crop a JPG, PNG or GIF in seconds for free! Crop a JPG, PNG or GIF in seconds for free! Crop IMAGE Crop JPG, PNG or GIF by defining a rectangle in pixels.

CAMERA" />. We need 2 fragments, one for camera preview and one to show cropped image. Create new fragment - ImageFragment  It's not an Activity or Fragment. It's just a FrameLayout which you can use anywhere and however you want in your app. There are some styling and customizations also available. 9. CropView. Cropview is an android image cropping library. 8. CropMe. This is an Android library for cropping images. Move images smoothly, and crop images precisely.

Comments
  • Did you at least read an exception? have you declared this activity in your AndroidManifest.xml
  • The result data is getting dumped in the Parent Activities onActivityResult() . Read the following : stackoverflow.com/questions/6147884/…