Created
May 1, 2020 22:00
-
-
Save wajahatkarim3/20c15f457a2127e4e32e66cd8db1bcfb to your computer and use it in GitHub Desktop.
The onActivityResult() method example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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