Welcome back.
And this video, we are going to implement the functionality to select a photo from gallery and also
to capture an image and put that into our happy place.
Let's right over here.
And we are Holmer is in our image for you here.
OK, then also this feature where we capture a photo with a camera and I'm just going to use the.
Generic image, of course, you can also test it on your phone, but I'm just going to use this image
here.
And press, OK, so there you are.
OK, so this is what we're going to implement in this video.
Let's get started.
So the first thing I want to add is the intent that allows me to go to the media storage and select
an image from there.
And where would I do that?
Well, I think the best part would be and the choose photo from gallery method.
OK, so here we currently just write a little toast or make a text here with a toast.
And instead of that, I want to execute something in there.
OK.
So of course we are going to need this bracket here, but everything else can go and in there we can
go ahead and now ask for the gallery content or create the gallery Internet gallery.
Internet is going to be a new intent.
And and action pick, so I'm picking something from the media store, that image is the media, that
external content urai.
All right, and then the next thing that I need to do is to start this intent and of course, with the
expectation that we will get a result.
So start activity.
Four result.
And when to intent, of course, which is art gallery intent, and we need to request court ID and I'm
going to use gallery here, even though we don't have gallery yet.
So we will need to create one and the gallery.
Variable that we still need this constant is going to be inside of a companion object, so we create
a companion object here.
Which we need for our static variable threat, and this gallery should be a static variable.
So private const Volle Kaleri, and I'm going to set that to one, as you might recall, we used companion
objects before.
They are really used just to create constants and to create our static variables.
All right.
Once we have done that.
We can implement all activity results method because that activity result starts and activity, but
then it waits for a result so that something comes back.
In this case, the image should come back because we are opening the gallery in order to select an image.
So let's implement that method here.
And I'm just going to make this public override fun on activity result.
So here a first check if the result, Kote.
OK, so the result of our activity, that result.
OK.
And if that's the case, then I want to check if the request was gallery.
So request code, visit our gallery, which means the variable that we just created, if that's the
request code.
And that is, by the way, the code that we passed in here when starting the activity for yourself,
because we can start multiple activities for result.
Right.
And each of them will, of course, give us different results.
And here we want to start a gallery intent and use this gallery code.
So this request code.
And if that one comes back, so if the activity result is from that call, then I want to do something
else.
And when I, for example, start an activity for result with a camera, just as an example where I just
would take an image.
OK, so what I do here is I check if the data that I get is not null and then I want to run some code,
by the way, request code result code and data are the ones.
So the variables that are given to us.
So these parameters here in our own activity result method.
OK, so if you were wondering where I get all of these values from and then if the data is not null
so we have an image, then we can.
Get the content derived from that data, so content your eyes, I'm going to call this variable and
that will be data dot data.
So.
Once I have the data, I can go ahead and try something, so try and what I will try here is to get
the image from the media store and from this content Urai.
OK, so I create a new variable here, which is going to be our selected image bitmap, and I used the
media store again media store that images that media that get pitmen.
So this method here is unfortunately deprecated since April twenty nine, but is still going to work
totally fine.
OK.
Unfortunately, they have made the deprecated, but they haven't give us give us a proper alternative
yet so that sometimes how you have to handle it.
So I'm using the content resolver here and the content.
You are right that we have.
Created before, so this one here.
So basically I'm passing the data.
To my get bitmap and I'm using this as the content resolver, well, what's that?
Well, the content resolver is the content resolver from our context, to be more precise, from our
context, which extends context.
So this our class itself, which is our.
Had heavy police activity, has this content resolved and we're passing that one all right, and once
we have the selected image, that map we can set, that's two hour drive place, image set, image bitmap.
And dare we select the selected image bitmap?
All right, and of course, because this is a try and catch, we also need to have the catch here,
which I'm just going to use the Iowa exception, because it's an input output exception that could arise
here, because we are getting input, which is our image here that we get from the gallery.
And I'm just going to.
Print the stack trace of this error and also make a little text, so a little toast here so that the
user knows, OK, something went wrong and I'm just going to say failed, maybe failed to load the image
from gallery.
All right.
That is our first request code.
OK, so we have it here and we check whether there is some data.
Then we take the data.
We store that in a variable called content urai, and then we try to get this bitmap from the media
store based on this your eye that we're creating.
So basically, we're just getting the data.
We're taking this data as an image in the form of a bitmap.
So here bitmap.
And then we can set the bitmap into our image view, place image, which is this complete image view,
which is this image view here that you can see.
And if that goes wrong, then we simply write a little text on the screen.
All right, so at this point, we can test this.
And see if we can select an image from the gallery and position that in our image, you.
All right, so let's start.
It's had an image and a photo from gallery.
Select the sohmer here.
And there we are.
All right, cool.
So the next thing I would like to do is to add the functionality with a camera, but I think we should
do that in the next video.
So see you there.