CropActivity is not starting in onActivityResult i

2019-08-24 20:52发布

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---
  }
  }

2条回答
萌系小妹纸
2楼-- · 2019-08-24 21:02

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

查看更多
Deceive 欺骗
3楼-- · 2019-08-24 21:08

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);
查看更多
登录 后发表回答