Skip to content

Instantly share code, notes, and snippets.

@wajahatkarim3
Created May 1, 2020 22:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wajahatkarim3/20c15f457a2127e4e32e66cd8db1bcfb to your computer and use it in GitHub Desktop.
Save wajahatkarim3/20c15f457a2127e4e32e66cd8db1bcfb to your computer and use it in GitHub Desktop.
The onActivityResult() method example
class MainActivity : AppCompatActivity()
{
// Other methods of your Activity
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
// Capture Image with Camera
if (requestCode == CAMERA_IMAGE_REQUEST && resultCode == Activity.RESULT_OK) {
if (data != null) {
val imageBitmap = data.getExtras().get("data") as Bitmap
// Do Something with image
} else {
// Couldn't take camera image
}
}
// Pick from Gallery
else if (requestCode == GALLERY_IMAGE_REQUEST && resultCode == Activity.RESULT_OK) {
if (data != null) {
Uri contentURI = data.getData();
// Decoding and loading Image from this URI
}
else {
// Couldn't pick image from Gallery. Maybe user cancelled it or maybe file is corrupt.
}
}
// Custom Activity Result
else if (requestCode == MY_OTHER_ACTIVITY_REQUEST && resultCode == Activity.RESULT_OK) {
if (data != null) {
var myValue = data.getStringExtra("myValueKey")
// Do something with this myValue
}
else {
// Maybe user cancelled it.
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment