Yes, that's correct. The startActivityForResult() method has been deprecated in API level deprecated in API level deprecated in API level deprecated in API level deprecated in API level Q (android 10.0). Instead, you should use registerForActivityResult() method introduced in API level registerForActivityResult() to handle the results from an Activity or a Permission request.
Here's how you can update the previous code to use the registerForActivityResult() method:
import android.Manifest
import android.app.Activity
import android.content.ContentResolver
import android.content.Intent
import android.content.pm.PackageManager
import android.net.Uri
import android.os.Bundle
import android.provider.MediaStore
import android.widget.Button
import android.widget.Toast
import androidx.activity.result.ActivityResult
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import java.util.*
class MainActivity : AppCompatActivity() {
private val PERMISSION_REQUEST_CODE = 100
private val PICK_IMAGE_REQUEST_CODE = 200
private lateinit var getContent : ActivityResultLauncher<String>
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
getContent = registerForActivityResult(ActivityResultContracts.GetContent()){ result: Uri? ->
result?.let {
// Add the image to the database
val image = Images(it.toString(), Date())
addImage(image)
}
}
val button = findViewById<Button>(R.id.select_image_button)
button.setOnClickListener {
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this,
arrayOf(Manifest.permission.READ_EXTERNAL_STORAGE), PERMISSION_REQUEST_CODE)
} else {
getContent.launch("image/*")
}
}
}
private fun addImage(image: Images) {
// Add the image to the database
val dbHelper = ImageDBHelper(this)
dbHelper.addImage(image)
}
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<String>, grantResults: IntArray) {
when (requestCode) {
PERMISSION_REQUEST_CODE -> {
if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
getContent.launch("image/*")
} else {
Toast.makeText(this, "Permission denied", Toast.LENGTH_SHORT).show()
}
return
}
}
}
}
Here, we first define a variable getContent of type ActivityResultLauncher to handle the result of launching an intent to get content. In the onCreate() method, we register the callback using registerForActivityResult() method which takes a parameter of type ActivityResultContracts.GetContent() that returns an Intent to select content from the device.
In the onRequestPermissionsResult() method, we handle the permission result as before. If the result is granted, we launch the getContent ActivityResultLauncher while passing "image/*" as the parameter to select images from the gallery.
In the callback of the getContent ActivityResultLauncher, we get the URI of the selected image and use it to create an Images object. We then add this object to the database using the addImage() method.
Make sure to also add the necessary permissions to your AndroidManifest.xml file:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />