CanHub / Android-Image-Cropper

Image Cropping Library for Android, optimised for Camera / Gallery.
Apache License 2.0
1.21k stars 249 forks source link

[Feat] - Get Photo From Camera with CropImage.activity() #295

Closed EnRiX84 closed 2 years ago

EnRiX84 commented 2 years ago

Hi Canato, please can you help me?

I have developed a cropimage that takes a photo from camera but the follow code

private void showDialogChoosePhoto() {
    // start picker to get image for cropping and then use the image in cropping activity
    CropImage.activity()
        .setGuidelines(CropImageView.Guidelines.ON)
        .setActivityTitle(getString(R.string.tab_photo))
        .setCropShape(CropImageView.CropShape.RECTANGLE)
        .setAspectRatio(1,1)
        .start(getActivity(), this);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

 // HERE INTENT DATA IS ALWAYS NULL FROM CAMERA

}

don't works with android 11 using latest release. It works with image from gallery, and from SD card o Storage but does not works with photo.

I have used this version implementation 'com.theartofdev.edmodo:android-image-cropper:2.8.+' with java and it worked fine.

I have followed the guide to convert old code to new code, using latest library but does not work yet.

Somebody could help me?

Sorry for my English and enjoy developing!!!

Thanks, Enrico

Canato commented 2 years ago

Hey, sorry for the lack of answers here.

I'm on vacation and traveling until 18/01. Just after this I will have the attention of this repository.

Do not need to wait for me, just letting people know as I'm the main maintenance

Canato commented 2 years ago

Hey @EnRiX84 could you please use the latest version, 4, and use the Activity Contracts?

EnRiX84 commented 2 years ago

@Canato THANK YOU VERY MUCH! i have resolver my problem followed your suggestion. Post Here my code, maybe I'll can someone else... I have used this in a FRAGMENT:

private ActivityResultLauncher<CropImageContractOptions> cropImage;
private Uri resultUri;
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        cropImage = registerForActivityResult(new CropImageContract(), new ActivityResultCallback<CropImageView.CropResult>() {
            @Override
            public void onActivityResult(CropImageView.CropResult result) {
                if (result.isSuccessful()) {
                    try {
                        resultUri = result.getUriContent();
                        thumbnail = MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(), resultUri);
                        if (thumbnail != null) {
                            dialogCircleImageView.setImageBitmap(thumbnail);
                        } else {
                            new ToastCustom().toastIconError("Failed to load image", getActivity());
                        }
                    } catch (IOException e) {
                        Log.e("MediaStore Error:", e.getMessage());
                        new ToastCustom().toastIconError(e.getMessage(), getActivity());
                        e.printStackTrace();
                    }
                    String uriFilePath = result.getUriFilePath(getContext(), true);
                } else {
                    // an error occurred
                    Exception e = result.getError();
                    new ToastCustom().toastIconError(e.getMessage(), getActivity());
                    Log.e("Camera Error:", e.getMessage());
                }
            }
        });
        super.onCreate(savedInstanceState);
        root = inflater.inflate(R.layout.activity_profile_store, container, false);
...
...
...
/*Call this method in onclicklistener in one button*/
 private void showDialogChoosePhoto() {
        // start picker to get image for cropping and then use the image in cropping activity
        startCameraWithoutUri(cropImage);
    }

    private void startCameraWithoutUri(ActivityResultLauncher<CropImageContractOptions> cropImage) {
        CropImageContractOptions options = new CropImageContractOptions(null, new CropImageOptions())
                .setScaleType(CropImageView.ScaleType.CENTER)
                .setCropShape(CropImageView.CropShape.RECTANGLE)
                .setCropMenuCropButtonIcon(R.drawable.ic_photo_camera)
                .setGuidelines(CropImageView.Guidelines.ON)
                .setActivityTitle(getString(R.string.tab_photo)
                );
        cropImage.launch(options);
    }

I hope is usefull for some java developer...

Thank you @Canato !