1

The output URI from crop() is always null. It's worth noting the result code is RESULT_CANCELLED for requestCode == Crop.REQUEST_CROP.

onActivityResult function:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == REQUEST_TAKE_PHOTO && resultCode == RESULT_OK) {
       // tell mediaplayer to add pic to is database
        galleryAddPic();
        // Use mPhotopath to start cropping asynchronously
        mPhotoCropUri = null;
        Crop.of(mPhotoURI, mPhotoCropUri).asSquare().start(this);
    } else if (requestCode == Crop.REQUEST_CROP && resultCode == RESULT_OK) {
        Log.e("Crop", mPhotoCropUri.toString());
    }
}

I am calling dispatchTakePictureIntent() to take a picture and store it. Here is the code:

private void dispatchTakePictureIntent() {
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    // Ensure that there's a camera activity to handle the intent
    if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
        // Create the File where the photo should go
        File photoFile = null;
        try {
            photoFile = createImageFile();
        } catch (IOException ex) {
            // Error occurred while creating the File

        }
        // Continue only if the File was successfully created
        if (photoFile != null) {
            mPhotoURI = FileProvider.getUriForFile(this,
                    "com.example.android.fileprovider",
                    photoFile);
            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, mPhotoURI);
            startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
        }
    }
}

private File createImageFile() throws IOException {
    // Create an image file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new java.util.Date());
    String imageFileName = "JPEG_" + timeStamp + "_";
    File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
    File image = File.createTempFile(
            imageFileName,  /* prefix */
            ".jpg",         /* suffix */
            storageDir      /* directory */
    );

    // Save a file: path for use with ACTION_VIEW intents
    mPhotoPath = "file:" + image.getAbsolutePath();
    return image;
}

private void galleryAddPic() {
    Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
    File f = new File(mPhotoPath);
    Uri contentUri = Uri.fromFile(f);
    mediaScanIntent.setData(contentUri);
    this.sendBroadcast(mediaScanIntent);
}

After storing the picture, I pass the URI of the stored picture to the crop function of the Android-crop library.

Daniel
  • 2,355
  • 9
  • 23
  • 30
Saumya
  • 83
  • 11

0 Answers0