I have the MainActivity from where I am starting the SecondActivity.
There, I select an image from gallery and I want to push it back to the MainActivity.
MainActivity:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
...
srButton = findViewById(R.id.super_resolution)
srButton.setOnClickListener { view: View ->
val intent = Intent(this, SecondActivity::class.java)
startActivity(intent)
val bitmap = getIntent().getParcelableExtra<Bitmap>("bitmap")
SecondActivity:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_selected_image)
image = findViewById(R.id.the_selected_image);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_DENIED) {
val permissions = arrayOf(Manifest.permission.READ_EXTERNAL_STORAGE)
requestPermissions(permissions, SelectedImage.PERMISSION_CODE)
} else {
chooseImageGallery()
}
} else {
chooseImageGallery()
}
}
// Receiver for camera
private val getResultCamera =
registerForActivityResult(
ActivityResultContracts.StartActivityForResult()
) {
if (it.resultCode == Activity.RESULT_OK) {
image.setImageURI(it.data?.data)
imageUri = it.data?.data as Uri
bitmap = uriToBitmap(imageUri!!)!!
image.setImageBitmap(bitmap)
val intent = Intent(this, MainActivity::class.java)
//intent.data = imageUri;
intent.putExtra("bitmap", bitmap)
startActivity(intent)
}
}
private fun chooseImageGallery() {
val intent = Intent(Intent.ACTION_PICK)
intent.type = "image/*"
getResultCamera.launch(intent)
}
Right now, I am receiving null for the bitmap.
Also, when I select the image from the gallery , the screen goes to MainActivity, so the user can't see the activity_selected_image
layout (hence , the image)