Yalantis / uCrop

Image Cropping Library for Android
https://yalantis.com/blog/introducing-ucrop-our-own-image-cropping-library-for-android/
11.9k stars 2.16k forks source link

Result callback not called in onActivityResult #471

Open bittentech opened 6 years ago

bittentech commented 6 years ago

I am testing the library on Android Oreo 8.1.0. I am calling the UCrop start() method after choosing an image from the gallery in onActivtyResult(). But after I finish cropping the image by clicking the right tick, nothing happens, the activity just finishes, onAcitivtyResult() is not being called. Here is my code: @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if(requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) { filePath = data.getData(); Toast.makeText(getContext(),"Image selected.", Toast.LENGTH_SHORT).show(); cropImage(); } else if (resultCode == RESULT_OK && requestCode == 1) { filePath = UCrop.getOutput(data); Toast.makeText(getContext(),"Image cropped.", Toast.LENGTH_SHORT).show(); } else if (resultCode == UCrop.RESULT_ERROR) { final Throwable cropError = UCrop.getError(data); Toast.makeText(getContext(),"Error: "+cropError, Toast.LENGTH_SHORT).show(); } } private void cropImage() { UCrop.of(filePath, filePath) .withAspectRatio(16, 16) .withMaxResultSize(500, 500) .start(getActivity(),1); }

BapediBoupi commented 5 years ago

From your code it seems that you're calling it from within a Fragment. In which case you need to use the start() function that takes a context, the fragment and the request code:

public void start(@NonNull Context context, @NonNull androidx.fragment.app.Fragment fragment, int requestCode)

cyberformed commented 5 years ago

I am also facing the same issue in Oreo OS. Can anybody resolve the issue?

programmerpinggiran commented 5 years ago

please refer to 335

zohabAli commented 3 years ago

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)