Ucrop onActivityMethod not working

1.8k Views Asked by At

I used below code from ucrop library and crop window is showing up.

 Uri destinationUri = Uri.fromFile(new File(myContext.getCacheDir(), "IMG_" + System.currentTimeMillis()));
            UCrop.of(sourceUri, destinationUri)
                     .withMaxResultSize(maxWidth, maxHeight)
                    .start(myContext);

but, control is not going inside onActivityResult method. If/Else both not working.

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_OK && requestCode == UCrop.REQUEST_CROP) {
        final Uri resultUri = UCrop.getOutput(data);
    } else if (resultCode == UCrop.RESULT_ERROR) {
        final Throwable cropError = UCrop.getError(data);
    }
}

Any idea whats wrong, did I missed something?

3

There are 3 best solutions below

0
On BEST ANSWER
Uri destinationUri = Uri.fromFile(new File(myContext.getCacheDir(), "IMG_" + System.currentTimeMillis()));
                UCrop.of(sourceUri, destinationUri)
                .withMaxResultSize(1080, 768) // any resolution you want
                .start(mContext, YourFragment/YourActivity.this);
0
On

Changing calling code to below works.

    Uri destinationUri = Uri.fromFile(new File(myContext.getCacheDir(), "IMG_" + System.currentTimeMillis()));
    startActivityForResult(UCrop.of(sourceUri, destinationUri)
             .withMaxResultSize(maxWidth, maxHeight)
            .getIntent(getContext()),UCrop.REQUEST_CROP);
0
On

I had to pass context and fragment so this is how I did in kotlin

UCrop.of(sourceFile.toUri(), destinationFile.toUri())
   .withAspectRatio(1.toFloat(), 1.toFloat())
   .start(requireContext(), this)