CropActivity is not starting in onActivityResult inside Fragment

青春壹個敷衍的年華 提交于 2020-01-16 20:56:08

问题


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

回答1:


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




回答2:


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);


来源:https://stackoverflow.com/questions/50180119/cropactivity-is-not-starting-in-onactivityresult-inside-fragment

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!