0

This is my code:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_crop);

    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    startActivityForResult(intent,CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE);
}

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // handle result of CropImageActivity
    if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) {
        CropImage.ActivityResult result = CropImage.getActivityResult(data);
        if (resultCode == RESULT_OK) {
            ((ImageView) findViewById(R.id.quick_start_cropped_image)).setImageURI(result.getUri());
            Toast.makeText(
                    this, "Cropping successful, Sample: " + result.getSampleSize(), Toast.LENGTH_LONG)
                    .show();
        } else if (resultCode == CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE) {
            Toast.makeText(this, "Cropping failed: " + result.getError(), Toast.LENGTH_LONG).show();
        }
    }
}

When I take a picture my app crashes. The exception on my phone is:

java.lang.RuntimeException: Failure delivering result 
ResultInfo{who=null, request=203, result=-1, data=Intent { act=inline- 
data (has extras) }} to activity 
{com.example.michael.matcalc/com.example.michael.matcalc.Crop}: 
java.lang.NullPointerException: Attempt to invoke virtual method 'android.net.Uri 
com.theartofdev.edmodo.cropper.CropImage$ActivityResult.getUri()' on a 
null object reference
at android.app.ActivityThread.deliverResults(ActivityThread.java:4355)
...

I think that something goes wrong with the data. It crashes at this line:

((ImageView) findViewById(R.id.quick_start_cropped_image)).setImageURI(result.getUri());

The library I use is this: https://github.com/ArthurHub/Android-Image-Cropper

I would appreciate if somebody could help me.

ישו אוהב אותך
  • 28,609
  • 11
  • 78
  • 96

1 Answers1

1

This is because you're incorrectly using the library. You need to use the following code:

CropImage.activity()
  .setGuidelines(CropImageView.Guidelines.ON)
  .start(this);

instead of:

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent,CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE);

It is because when you're calling Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); you're not specifically tell the Crop library to handle the image.

ישו אוהב אותך
  • 28,609
  • 11
  • 78
  • 96