0

I am calling an intent to select an image and later to crop the image into aspect ratio(1,1), but when i run the app the gallery is opening but when i select the image it closes and gets back to the Fragment.

Below is the code of my Button's on click listener

    mImageBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {


            Intent galleryIntent = new Intent();
            galleryIntent.setType("image/*");
            galleryIntent.setAction(Intent.ACTION_GET_CONTENT);

            startActivityForResult(Intent.createChooser(galleryIntent, 
   "SELECT IMAGE"), GALLERY_PICK);
  }
    });

this is the code for onActivityResult

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

    if(requestCode == GALLERY_PICK && resultCode == RESULT_OK){

        Uri imageUri = data.getData();

        CropImage.activity(imageUri)
                .setAspectRatio(1, 1)
                .setMinCropWindowSize(500, 500)
                .start(getActivity());

        Toast.makeText(SettingsActivity.this, imageUri, 
 Toast.LENGTH_LONG).show();

    }
   @Override
  public void onActivityResult(int requestCode, int resultCode, Intent data) 
 {
  if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) {
 CropImage.ActivityResult result = CropImage.getActivityResult(data);
 if (resultCode == RESULT_OK) {
  Uri resultUri = result.getUri();
   } else if (resultCode == CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE) 
{
  Exception error = result.getError();
  }
 }
}

Crop activity is not starting and the app goes back to the fragment screen when i select the pic

Below is the code of MainActivity where i have used bottom navigation view and used OnActivity result as well

  private void showPlacePicker() {
    PlacePicker.IntentBuilder builder = new PlacePicker.IntentBuilder();
    try {
        startActivityForResult(builder.build(this), PLACE_PICKER_REQUEST);
    } catch (Exception e) {
        Log.e(TAG, e.getStackTrace().toString());
    }
 }


  @Override
   public void onActivityResult(int requestCode, int resultCode, Intent 
   data) {
    if (requestCode == PLACE_PICKER_REQUEST) {
        if (resultCode == RESULT_OK) {
 ----code---
  }
  }
depp
  • 25
  • 6
  • Where are first and second `onActivityResult()` methods located? – Sergey Glotov May 04 '18 at 17:21
  • First onActivityResult is located in my MainActivity and this one is in the SettingsFragmetn – depp May 04 '18 at 17:29
  • My mainActivity has a bottomNavigationView in which there is this settings fragment where i am trying to call this method – depp May 04 '18 at 17:32
  • Overriding `onActivityResult` in your fragment seems to have the opposite of the intended behavior in that it appears it's executed first by the activity and then the fragment. Are you calling the super method in the activity implementation? – rosghub May 04 '18 at 17:38
  • i have added the code from mainactivity where i have called the OnActivityResult . i havent used super in main activity – depp May 04 '18 at 18:21
  • if you want i can paste the whole code above as well – depp May 04 '18 at 18:22
  • "i havent used super in main activity". Then it's probably duplicate of https://stackoverflow.com/questions/6147884/onactivityresult-is-not-being-called-in-fragment – Sergey Glotov May 04 '18 at 22:12
  • i have used it in fragment only see the code above public void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); – depp May 05 '18 at 03:00
  • i have tried almost everyway but still its not calling the crop method from my fragment. SettingsFragment.this.startActivityForResult(Intent.createChooser(galleryIntent, "SELECT IMAGE"), GALLERY_PICK); this is also not working – depp May 05 '18 at 03:12
  • Anyone ...i have almost tried everything from other threads please help me out – depp May 05 '18 at 07:25
  • @SergeyGlotov i have used super in the code above can you please have a look at the code – depp May 05 '18 at 09:34
  • @jefferson i have used ..public void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); in my Fragment which i found in some old questions here but still its of no use – depp May 05 '18 at 09:36
  • You should use `super.onActivityResult()` in activity code – Sergey Glotov May 05 '18 at 15:02
  • @depp call super in your activity implementation – rosghub May 05 '18 at 17:02
  • Caused by: android.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.jimmy.chatapplication/com.theartofdev.edmodo.cropper.CropImageActivity}; have you declared this activity in your AndroidManifest.xml? i am still getting this error and i have changed the code as you guys have told to public void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == PLACE_PICKER_REQUEST) { if (resultCode == RESULT_OK) { ------rest of the code } – depp May 05 '18 at 17:43
  • @SergeyGlotov sir, can you please help me out with this – depp May 06 '18 at 05:25
  • are you getting any exception or your fragment's onActivityResult not working? – Fahed Yasin May 06 '18 at 08:03
  • @FahedYasin when i added super in my main activity it is showing FATAL EXCEPTION: main java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=65537, result=-1, data=Intent { dat=content://com.android.providers.media.documents/document/image:2499 flg=0x1 }} to activity {com.../.MainActivity}: android.content.ActivityNotFoundException: Unable to find explicit activity class {com...../com.theartofdev.edmodo.cropper.CropImageActivity}; have you declared this activity in your AndroidManifest.xml? – depp May 06 '18 at 09:54
  • @FahedYasin if i remove super from main class then it is showing 05-06 15:21:06.559 4315-4315/? E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.jimmy.chatapplication, PID: 4315 java.lang.StackOverflowError – depp May 06 '18 at 09:55
  • @depp are you using [Android-Image-Cropper](https://github.com/ArthurHub/Android-Image-Cropper) library? – Rahul Sahni May 07 '18 at 13:02
  • @RahulSahni i am using 'com.theartofdev.edmodo:android-image-cropper:2.7.0' – depp May 07 '18 at 13:51
  • @depp please read the [documentation] (https://github.com/ArthurHub/Android-Image-Cropper#using-activity) of the library, it is written that one should not use getActivity() in fragment `// for fragment (DO NOT use `getActivity()`) CropImage.activity() .start(getContext(), this);` – Rahul Sahni May 07 '18 at 13:58
  • @jefferson i am using super.onActivityResult(requestCode, resultCode, data); already and also i found on github to use .start(getContext(), this) ; to start activity but still its not even showing any error. My gallery opens and closes when i click on image and i am back to the fragment page. – depp May 07 '18 at 14:47

3 Answers3

0

I have used this library in one of my projects and in that I didn't explicitly create a image picker but instead let the library handle it for me. To do this, in your code

mImageBtn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {


        CropImage.activity()
            .setAspectRatio(1, 1)
            .setMinCropWindowSize(500, 500)
            .start(getContext());
  }
});

Handle the result:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) {
        CropImage.ActivityResult result = CropImage.getActivityResult(data);

        if (resultCode == RESULT_OK) {

            Uri imageUri = result.getUri();
            //handle the image



        } else if (resultCode == CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE) {
            //handle error

        } else if(resultCode == RESULT_CANCELED){
          //handle cancel case
        }
    }
}

For more information

Rahul Sahni
  • 465
  • 4
  • 12
0

In case of using appcompact fragment we cannot use .start(getActivity()) to start the crop activity. Instead of this use:

.start(getContext(), this)

Here is code look like in fragments:

CropImage.activity(uri).setGuidelines(CropImageView.Guidelines.ON).start(getContext(), this);
Gaurav sappal
  • 81
  • 1
  • 5
0

ActivityResult is more suitable :

    @Override
    public void onActivityResult(ActivityResult activityResult) {
      if (activityResult.getResultCode() == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) {
        CropImage.ActivityResult result = CropImage.getActivityResult(activityResult.getData());
}
if (activityResult.getResultCode() == RESULT_OK) {
                            Uri resultUri = result.getUri();
                        } else if (activityResult.getResultCode() == CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE) {
                            Exception error = result.getError();
                        }
                    }
                }
LJOBOY
  • 11
  • 4
  • 2
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Feb 01 '22 at 12:35