diff --git a/CHANGELOG.md b/CHANGELOG.md index 1e54525c..a9863af5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) - `CropImageContract` and `PickImageContract` - added dependency to `androidx.activity:activity-ktx:.2.3` +### Changed +- `CropImageActivity.onActivityResult` no longer receives any result. Override `onPickImageResult` instead. + ### Deprecated - deprecated old methods that depend on the deprecated `onActivityResult`. Use `CropImageContract` and `PickImageContract` instead. diff --git a/cropper/src/main/java/com/canhub/cropper/CropImage.kt b/cropper/src/main/java/com/canhub/cropper/CropImage.kt index b71280ac..b4b9848a 100644 --- a/cropper/src/main/java/com/canhub/cropper/CropImage.kt +++ b/cropper/src/main/java/com/canhub/cropper/CropImage.kt @@ -130,7 +130,7 @@ object CropImage { * * @param activity the activity to be used to start activity from */ - @Deprecated("use the OpenChooser ActivityResultContract instead") + @Deprecated("use the PickImageContract ActivityResultContract instead") fun startPickImageActivity(activity: Activity) { activity.startActivityForResult( getPickImageChooserIntent(activity), PICK_IMAGE_CHOOSER_REQUEST_CODE @@ -144,7 +144,7 @@ object CropImage { * @param context The Fragments context. Use getContext() * @param fragment The calling Fragment to start and return the image to */ - @Deprecated("use the OpenChooser ActivityResultContract instead") + @Deprecated("use the PickImageContract ActivityResultContract instead") fun startPickImageActivity(context: Context, fragment: Fragment) { fragment.startActivityForResult( getPickImageChooserIntent(context), PICK_IMAGE_CHOOSER_REQUEST_CODE diff --git a/cropper/src/main/java/com/canhub/cropper/CropImageActivity.kt b/cropper/src/main/java/com/canhub/cropper/CropImageActivity.kt index 40fc6b56..9d8dfaff 100644 --- a/cropper/src/main/java/com/canhub/cropper/CropImageActivity.kt +++ b/cropper/src/main/java/com/canhub/cropper/CropImageActivity.kt @@ -49,6 +49,8 @@ open class CropImageActivity : private var cropImageView: CropImageView? = null private lateinit var binding: CropImageActivityBinding + private val pickImage = registerForActivityResult(PickImageContract(), ::onPickImageResult) + public override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) @@ -68,7 +70,7 @@ open class CropImageActivity : CropImage.CAMERA_CAPTURE_PERMISSIONS_REQUEST_CODE ) } else { - CropImage.startPickImageActivity(this) + pickImage.launch(true) } } else if ( cropImageUri?.let { @@ -163,30 +165,25 @@ open class CropImageActivity : setResultCancel() } - @SuppressLint("NewApi") - public override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { - super.onActivityResult(requestCode, resultCode, data) - // handle result of pick image chooser - if (requestCode == CropImage.PICK_IMAGE_CHOOSER_REQUEST_CODE) { - if (resultCode == RESULT_CANCELED) setResultCancel() - if (resultCode == RESULT_OK) { - cropImageUri = CropImage.getPickImageResultUriContent(this, data) - // For API >= 23 we need to check specifically that we have permissions to read external - // storage. - if (cropImageUri?.let { + protected open fun onPickImageResult(resultUri: Uri?) { + if (resultUri == null) setResultCancel() + if (resultUri != null) { + cropImageUri = resultUri + // For API >= 23 we need to check specifically that we have permissions to read external + // storage. + if (cropImageUri?.let { CropImage.isReadExternalStoragePermissionsRequired(this, it) } == true && - CommonVersionCheck.isAtLeastM23() - ) { - // request permissions and handle the result in onRequestPermissionsResult() - requestPermissions( - arrayOf(Manifest.permission.READ_EXTERNAL_STORAGE), - CropImage.PICK_IMAGE_PERMISSIONS_REQUEST_CODE - ) - } else { - // no permissions required or already grunted, can start crop image activity - cropImageView?.setImageUriAsync(cropImageUri) - } + CommonVersionCheck.isAtLeastM23() + ) { + // request permissions and handle the result in onRequestPermissionsResult() + requestPermissions( + arrayOf(Manifest.permission.READ_EXTERNAL_STORAGE), + CropImage.PICK_IMAGE_PERMISSIONS_REQUEST_CODE + ) + } else { + // no permissions required or already granted, can start crop image activity + cropImageView?.setImageUriAsync(cropImageUri) } } } @@ -212,7 +209,7 @@ open class CropImageActivity : } else if (requestCode == CropImage.CAMERA_CAPTURE_PERMISSIONS_REQUEST_CODE) { // Irrespective of whether camera permission was given or not, we show the picker // The picker will not add the camera intent if permission is not available - CropImage.startPickImageActivity(this) + pickImage.launch(true) } else super.onRequestPermissionsResult(requestCode, permissions, grantResults) } diff --git a/sample/src/main/java/com/canhub/cropper/sample/extend_activity/app/SExtendActivity.kt b/sample/src/main/java/com/canhub/cropper/sample/extend_activity/app/SExtendActivity.kt index 0284cb04..1e8622f6 100644 --- a/sample/src/main/java/com/canhub/cropper/sample/extend_activity/app/SExtendActivity.kt +++ b/sample/src/main/java/com/canhub/cropper/sample/extend_activity/app/SExtendActivity.kt @@ -18,7 +18,6 @@ import com.example.croppersample.databinding.ExtendedActivityBinding internal class SExtendActivity : CropImageActivity(), SExtendContract.View { companion object { - fun start(activity: Activity) { ActivityCompat.startActivity( activity, @@ -68,11 +67,11 @@ internal class SExtendActivity : CropImageActivity(), SExtendContract.View { binding.rotateText.text = getString(R.string.rotation_value, counter) } - override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { - super.onActivityResult(requestCode, resultCode, data) + override fun onPickImageResult(resultUri: Uri?) { + super.onPickImageResult(resultUri) - if (requestCode == CropImage.PICK_IMAGE_CHOOSER_REQUEST_CODE && resultCode == RESULT_OK) { - binding.cropImageView.setImageUriAsync(cropImageUri) + if (resultUri != null) { + binding.cropImageView.setImageUriAsync(resultUri) } }