Skip to content

Commit

Permalink
update CropImageActivity
Browse files Browse the repository at this point in the history
  • Loading branch information
connyduck committed Jun 24, 2021
1 parent 2efd302 commit 7167e73
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 31 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
4 changes: 2 additions & 2 deletions cropper/src/main/java/com/canhub/cropper/CropImage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
45 changes: 21 additions & 24 deletions cropper/src/main/java/com/canhub/cropper/CropImageActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -68,7 +70,7 @@ open class CropImageActivity :
CropImage.CAMERA_CAPTURE_PERMISSIONS_REQUEST_CODE
)
} else {
CropImage.startPickImageActivity(this)
pickImage.launch(true)
}
} else if (
cropImageUri?.let {
Expand Down Expand Up @@ -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)
}
}
}
Expand All @@ -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)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
}
}

Expand Down

0 comments on commit 7167e73

Please sign in to comment.