Closed ArtRoman closed 2 months ago
Can you please create a PR?
The cropImage.launch()
method was changed in version 4.3.3
of the Android Image Cropper library. The old method used a options()
block to set the crop options, but this block is no longer supported. In the new method, you need to pass a CropImageContractOptions
object to the launch()
method. This object contains the crop options, including the output compression format.
However, in newer versions, you will need to use the new launch()
method.
Here is an example of how to use the new launch() method:
val cropImageOptions = CropImageOptions.Builder()
.setOutputCompressFormat(Bitmap.CompressFormat.PNG)
.build()
val cropImageContractOptions = CropImageContractOptions(
uri = imageUri,
cropImageOptions = cropImageOptions
)
cropImage.launch(cropImageContractOptions)
Here's the modified version of the code:
class MainActivity : AppCompatActivity() {
private val cropImage = registerForActivityResult(CropImageContract()) { result ->
if (result.isSuccessful) {
// Use the cropped image URI.
val croppedImageUri = result.uriContent
val croppedImageFilePath = result.getUriFilePath(this) // optional usage
// Process the cropped image URI as needed.
} else {
// An error occurred.
val exception = result.error
// Handle the error.
}
}
private fun startCrop() {
// Start cropping activity with guidelines.
cropImage.launch(
CropImageContractOptions(
cropImageOptions = CropImageOptions(
guidelines = Guidelines.ON
)
)
)
// Start cropping activity with gallery picker only.
cropImage.launch(
CropImageContractOptions(
pickImageContractOptions = PickImageContractOptions(
includeGallery = true,
includeCamera = false
)
)
)
// Start cropping activity for a pre-acquired image with custom settings.
cropImage.launch(
CropImageContractOptions(
uri = imageUri,
cropImageOptions = CropImageOptions(
guidelines = Guidelines.ON,
outputCompressFormat = Bitmap.CompressFormat.PNG
)
)
)
}
// Call the startCrop function when needed.
}
In this code, I've made the following changes:
AppCompatActivity
.croppedImageUri
to be more descriptive.getUriFilePath
to ensure proper context reference.cropImage.launch
is called by providing CropImageContractOptions
objects that contain the necessary options for each scenarioHere's the modified version of the code:
Thank you, I haven't got any problems with modifying code. I use most of available options, so my changelog is quite larger:
cropImage.launch(
options(uri) {
setGuidelines(CropImageView.Guidelines.ON_TOUCH)
setActivityTitle(getString(R.string.profile_select_image))
setCropShape(CropImageView.CropShape.RECTANGLE)
setFixAspectRatio(false)
setInitialCropWindowPaddingRatio(0f)
setCropMenuCropButtonTitle(getString(R.string.send))
setRequestedSize(Constants.MAX_PHOTO_SIZE, Constants.MAX_PHOTO_SIZE, CropImageView.RequestSizeOptions.RESIZE_INSIDE)
setOutputCompressFormat(Bitmap.CompressFormat.JPEG)
}
)
to
cropImage.launch(
CropImageContractOptions(
uri, CropImageOptions(
guidelines = CropImageView.Guidelines.ON_TOUCH,
activityTitle = getString(R.string.profile_select_image),
cropShape = CropImageView.CropShape.RECTANGLE,
fixAspectRatio = false,
initialCropWindowPaddingRatio = 0f,
cropMenuCropButtonTitle = getString(R.string.send),
outputRequestSizeOptions = CropImageView.RequestSizeOptions.RESIZE_INSIDE,
outputRequestWidth = Constants.MAX_PHOTO_SIZE,
outputRequestHeight = Constants.MAX_PHOTO_SIZE,
outputCompressFormat = Bitmap.CompressFormat.JPEG,
)
)
)
The issue is about misleading documentation in the project's welcome page of this repository, which may disappoint new library users.
@harshsingh-io thanks for the corrections. I've created a PR :)
@vanniktech I hope we can work together. I was not free at that moment. That's why I was not able to create PR of it.
Current documentation is misleading, this code in README.md will be reported with an "Unresolved reference" errors:
It requires new launch and options objects in 4.3.3 and later: