Search code examples
javaandroidxmlfirebasecrop

Unable to continue after cropping a image in CropActivity


I am using ArthurHub's library to crop a image and put it in a ImageButton or ImageView. Crop Activity starts smoothly but after cropping when I clicks to CROP button, my app reruns and opens MainActivity. It seems to me that there is no error in the code and also I have checked it 5-6 times.

Thanks!!!

Here is my code...

public class AddActivity extends AppCompatActivity {
private ImageButton add_image_button;

private Uri imageUri;
private String imageUrl;

private String recordPermission = Manifest.permission.RECORD_AUDIO;
private String readExternalStorage = Manifest.permission.READ_EXTERNAL_STORAGE;
private String recordFile;
private int PERMISSION_CODE = 7;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_add);

    add_image_button = findViewById(R.id.add_image_button);
    
    CropImage.activity(imageUri).start(AddActivity.this);

    add_image_button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            checkReadExternalStoragePermissions();
            CropImage.activity(imageUri).start(AddActivity.this);
        }
    });

}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE && resultCode == RESULT_OK) {
        CropImage.ActivityResult result = CropImage.getActivityResult(data);
        imageUri = result.getUri();

        add_audio_button.setImageURI(imageUri);
    }else {
        Toast.makeText(this, "Try again...", Toast.LENGTH_SHORT).show();
        startActivity(new Intent(AddActivity.this, MainActivity.class));
        finish();
    }

}

private boolean checkRecordPermissions() {
    if (ActivityCompat.checkSelfPermission(this, recordPermission) == PackageManager.PERMISSION_GRANTED) {
        return true;
    } else {
        ActivityCompat.requestPermissions(this, new String[]{recordPermission}, PERMISSION_CODE);
        return false;
    }

}
private boolean checkReadExternalStoragePermissions() {
    if (ActivityCompat.checkSelfPermission(this, readExternalStorage) == PackageManager.PERMISSION_GRANTED) {
        return true;
    } else {
        ActivityCompat.requestPermissions(this, new String[]{readExternalStorage}, PERMISSION_CODE);
        return false;
    }

}

}


Solution

  • I have found out the error, there was a slight mistake in the code.

    I used add_audio_button.setImageUri(imageUri) instead of add_image_button.setImageUri(imageUri); here:

    if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE && resultCode == RESULT_OK) {
        CropImage.ActivityResult result = CropImage.getActivityResult(data);
        imageUri = result.getUri();
        add_audio_button.setImageURI(imageUri);