Due to the fact that StartActivityForResult is deprecated, In this tutorial, we gonna go through how you can be able to launch an Intent, pick an image from it and then load that image into an Image View on your app. Obviously you wanna have an image view in place for this, plus your attention. Another thing you really are going to get stuck without is to make sure your app is granted with a permission to read external storage. I have an article about this, you can check it here.
A while ago we had this method onActivityResult which i got used to, but came out to find it was deprecated. So the approach am gonna employ here is the fresh most technology available (at least i hope it is, coz this stuff change over time), so let's get started..
1. Create an Intent for the image selection process
The first thing we gonna do here is to create an intent with a picking action. The idea shouldn't be very new, as i assume you have used intents for quite a long time now. You've probably used an intent to start an Activity, moving from one to another, so let's make this similar process right here..
val intentYaPicha = Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI)
the variable i have just created, am gonna use it to launch an activity using that, but only this kind is the one you always see when you are selecting pictures to share on Instagram or Quora say. It's makes perfect sense as you can see we got a picking action right there.
2. Launching the Intent.
It's about to get strange in a second, because am going to use something i have not said a thing about, yet.
shughuliUchaguziPicha.launch(intentYaPicha)
you have every right to be asking, what are you looking at right now?. Well, cool answer to this, you have every answer you need regarding the intentYaPicha in the parenthesis, i just spoke about it, a couple of lines above. As the header says, we launched it. The real puzzle you and me have got to solve now is with the shughuliUchaguzPicha. Keep in mind, it's just a name, so i expect yours to be something other than mine, or it could be the same, no hard feelings.
registerForActivityResult()
So, about shughuliUchaguziPicha above, it's a result of some kind of a method, the method really does a lot, from managing run time permissions (i wrote about these here) to this thing we are trying to achieve right now. So make a space a couple of lines below the launching we've just done and define something like this
private var shughuliUchaguziPicha = registerForActivityResult(ActivityResultContracts.StartActivityForResult()){ mrejesho ->
if (mrejesho.resultCode == Activity.RESULT_OK){
if (mrejesho.data != null){
// you get the uri from mrejesho.data so here would be a fine place for me to load an image.
}
}
} Let's go through details you see inside it. We launched an activity from which we pick an image and eventually the activity closes down as soon as we are done with the picking. We eventually need a way to recall what we selected from the activity to our activity on our mobile app. So this is where the lambda expression comes in, it's commonly denoted as it but i chose to rename mine to mrejesho, it's some Swahili, meaning feedback. We could learn a lot from this feedback we get right here. It's the one thing that tells us if an image was picked or not. In some instances, a user could decide not to, and if we had no way to detect this, we could go ahead and load a null into an image view. This causes a crash. Definitely.
A requestCode we got used to when using onActivityResult is handled silently on this newer fashion. So you really do not need to bother no more with setting up the request codes, but rather we have got to check some stuff still.
resultCode
Just like the traditional ways we had, you wanna check the result from the activity we launched, if it's okay or not cool. For positive cases, you also should take a step further into checking with the data that is being carried along with it.
data
It's safer to confirm that the variable has something in it, so here we check if it's null or for better if it is not, then good for us. It's the one place we pick the uri of the image picked and then you go ahead and do with it whatever you please. I am going to load it to an imageView as i promised. You could have other uses with it, say you wanna upload the picture to the cloud.
It's better for me to hope you have learned something from this article, visit some other of mine. I have written a couple now, they are really educative, so feel very free to move around and see what i have prepared for you. Leave a comment on what you would like me to write about.
Stay awesome
#Sam
0 Comments