Need help, I'm stuck at how to convert kotlin function from CanHub Image Cropper to Java. It include two code, Activity result and the calling method
the original kotlin code is like this for the activity result:
private val cropImage = registerForActivityResult(CropImageContract()) { result ->
if (result.isSuccessful) {
// use the returned uri
val uriContent = result.uriContent
val bitmap = MediaStore.Images.Media.getBitmap(myActivity.contentResolver, uriContent)
imageView.setImageBitmap(bitmap)
} else {
// an error occurred
val exception = result.error
}
}
and this is how the calling method in kotlin:
cropImage.launch(
options {
setImageSource(
includeGallery = true,
includeCamera = true
)
setAspectRatio(150,150)
setGuidelines(CropImageView.Guidelines.ON)
setOutputCompressFormat(Bitmap.CompressFormat.JPEG)
setOutputCompressQuality(85)
}
)
I try to convert the code into java since my project right now is Java and older library is not compatible with Android 11
here is my activity result converted into java, I don't know if it's the right one but at least it has no error:
ActivityResultLauncher<CropImageContractOptions> cropImage = registerForActivityResult(
new CropImageContract(),
result -> {
Uri uriContent = result.getUriContent();
Bitmap bitmap = null;
try {
bitmap = MediaStore.Images.Media.getBitmap(myActivity.getContentResolver(), uriContent);
} catch (IOException e) {
e.printStackTrace();
}
imageView.setImageBitmap(bitmap);
});
but I'm stuck at the calling method:
CropImageContractOptions cropImageContractOptions = ? /* <-- what to put here?? */
cropImage.launch(cropImageContractOptions);
I have tried to use option but it just error unresolved variable
I have taking a look at this: How to croping image with source just from camera with Canhub Android Image Cropper with Java
But for some reason it does not even work at all for me
Is CanHub Image Cropper is not compatible with Java at all?
CodePudding user response:
Make sure you added last version
dependencies {
implementation "com.vanniktech:android-image-cropper:4.5.0"
}
Use this code
ActivityResultLauncher<CropImageContractOptions> cropImage = registerForActivityResult(new CropImageContract(), result -> {
if (result.isSuccessful()) {
Bitmap cropped = BitmapFactory.decodeFile(result.getUriFilePath(getApplicationContext(), true));
}
});
CropImageOptions cropImageOptions = new CropImageOptions();
cropImageOptions.imageSourceIncludeGallery = false;
cropImageOptions.imageSourceIncludeCamera = true;
CropImageContractOptions cropImageContractOptions = new CropImageContractOptions(/*pass you image uri*/, cropImageOptions);
cropImage.launch(cropImageContractOptions);