I'm new in programming, some technolgies i'm trying learn by myself, I've watched a lot of video new activity result Api, and i'm trying migrate in my project. everything fine , but trouble is the missing request code, what have to do or use for request code .
here in project old Activity result trying to Crop Image
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { /// неед
super.onActivityResult(requestCode, resultCode, data)
if (resultCode == Activity.RESULT_OK) {
if (requestCode == REQUEST_GALLERY) {
data?.data.let {
if (it != null) {
imageUri = it
cropImage(it) }
}
} else if (requestCode == UCrop.REQUEST_CROP) {
val resultUri = data?.let { UCrop.getOutput(it) }
binding.image.loadUrl(resultUri.toString())
}
} else {
toastMessage(requireContext(), getString(R.string.fail_download_image))
}
}
private fun openGallery() { /// галлерея
val intent = Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI)
startActivityForResult(intent, REQUEST_GALLERY)
}
here I'm migrating Activirty Result Api
val resultContract = registerForActivityResult(ActivityResultContracts.StartActivityForResult()){result: ActivityResult? ->
if (result?.resultCode == Activity.RESULT_OK) {
if (requestCode == REQUEST_GALLERY) {
result.data?.data.let {
if (it != null) {
imageUri = it
cropImage(it) }
}
} else if (result.requestCode == UCrop.REQUEST_CROP) {
val resultUri = result.data?.let { UCrop.getOutput(it) }
binding.image.loadUrl(resultUri.toString())
}
} else {
toastMessage(requireContext(), getString(R.string.fail_download_image))
}
}
here in Activity Result Api request code is unresolved references , what to use instead of request code???
I think I'm late with the answer. If you really need to get the request code in Activity Result API you should do something like that:
// You can use any request code
val REQUEST_GALLERY = 1001
val resultContract = registerForActivityResult(ActivityResultContracts.StartActivityForResult()){result: ActivityResult? ->
val requestCode = result.data?.extras?.getInt(REQUEST_GALLERY.toString())
if (result?.resultCode == Activity.RESULT_OK) {
if (requestCode == REQUEST_GALLERY) {
result.data?.data.let {
if (it != null) {
imageUri = it
cropImage(it) }
}
} else if (result.requestCode == UCrop.REQUEST_CROP) {
val resultUri = result.data?.let { UCrop.getOutput(it) }
binding.image.loadUrl(resultUri.toString())
}
} else {
toastMessage(requireContext(), getString(R.string.fail_download_image))
}
}