I'm updating the way that my app get images from gallery. I know that in the old way, which was startActivityForResult(), we can send a custom requestCode. However, in the ActivityResultLauncher we can't, right? It is automatically generated, so, with that being said, I'll show you my problem:
class TesteActivity: AppCompatActivity() {
lateinit var resultLauncherPhotoPicker: ActivityResultLauncher<Intent>
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.teste)
val imageView = findViewById<ImageView>(R.id.myImageView)
resultLauncherPhotoPicker = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
if (result.resultCode == RESULT_OK) {
val data = result.data
val imageUri = data?.data
imageView.setImageURI(imageUri)
}
}
myButton.setOnClickListener {
val intent = Intent(Intent.ACTION_GET_CONTENT)
intent.type = "image/*"
resultLauncherPhotoPicker.launch(intent)
}
}
}
This is the code snippet that don't run on my app, I get a error saying that the requestCode can't be higher than 16bits. When I debug the method that checks the requestCode, it does go over than 16 bits. But the main problem is: Why? If I can't send a custom requestCode, why is the requestCode automatically generated by the launcher wrong? I tried this exact snippet on a new empty project on my Android Studio and it WORKED. The same thing. What am I missing?