Welcome back and this video, we are going to implement the actual camera functionality so that the
camera pops up, we take an image and then this image is stored here in this image of you and what is
displayed there.
So selecting a camera, then this pops up, as you can see, and then you can move around to take another
image.
So for example, hold W while holding all will move you forward.
So let's make an image or take an image of that cat.
I'm going to save that.
And you can see now we have that image up there.
OK, so that's the result of this video.
In order to get there, I want to make a couple of changes to the code that we created last time.
First, because there are a couple of things that aren't working perfectly yet, and that is specifically
because of this line here.
And we are accessing the external storage, which is fine.
We want to do that, but it works differently in the latest API levels.
OK, so an API, a level 30.
If you hover over it, well, it doesn't displayed here, unfortunately, but basically this is not
required anymore in the latest API levels.
So if you read this code on your older device or on a device that uses an older Android version, this
will work.
OK, so this statement here will work.
But because we want this to work on new devices as well, we need to comment this line out.
And now this toast should work.
Before we do that, there is one more thing that we also need to change real quick.
And that is the same thing when we ask for the permission, when we click on the gallery.
OK, so here, instead of asking for multiple permissions, we are only going to ask for the external
permission.
Now, with that in place, we need to make a change here to our multiple missions listener here as well,
because we are only listening for one permission.
And that will be the permission listener that we need to implement now.
Now, of course, the methods don't fit correctly anymore, so we don't need to have this one permission
checked anymore, however.
Well, let's just implement the methods that are going to be there by default.
So implement members.
Let's add all of those.
And these are going to be the ones that we will require here.
So on permission granted, I'm just going to display a toast.
You have the gallery permission now to select an image and then if the permission was denied, then
I'm going to display you have denied the storage permission to select image and then on permission rationale
shown.
So if we want to display to the user that, well, basically our rational dialogue permission where
we tell the user that he didn't activate the permission and we need it for the application.
OK, so this will be the changed code for our TV gallery.
And now there is one more thing that I would like to change, and that is this part here.
OK, so I use this report double exclamation mark, because while this is a nullable, right.
There's a question mark here.
Now you can do that, but this is not the cleanest way to do it because potentially report could be
empty and then, well, our code would crash.
So if you want to make sure that this is definitely going to work and not crash, even though maybe
report is empty, well, in that case, it won't execute the code.
But in order to really make sure that everything is fine, you can surround it with a let.
So you say report question mark, dot let and then in the brackets you execute the code that you wanted
to execute.
So now you will see that you don't need exclamation marks here anymore because we only execute this
code here if report is not empty.
So that's basically what we do with this report, not empty, then execute this.
And that's how you do it with this.
Aulet OK, so but now instead of displaying those Tostes, I want to actually well execute some interesting
code, which means I want to open up the camera and in order to open the camera, we need to start an
intent and you can do it using intent media store, DOT action, image capture.
And this allows us to capture an image.
Now we need to start that activity, but not just like that, but for a result or with a result, because
we want to also have the camera details while the image that was taken, basically, and we want to
store that now here I'm going to use the camera ID.
So this would be one.
Well, you can you can use any number, any request code.
No, I'm just going to enter camera here and I will also create a companion object for that.
OK, so you can do that at the top of your class or at the bottom either way is fine.
So I'm just going to add a companion object which will contain my fixed variable, so on my constant,
so to speak.
OK, so const Vul will be camera and I'm going to set that value to one.
OK, so this is just a cleaner way, you could have, of course, just entered one, but the idea is
that you can have multiple activities for result and in our case, we will have multiple start activities
for result.
The other one will be, for example, when we select an image from the gallery, then we will have a
different request code.
So this one should be a different one because we need to know what the activity result intent was.
So what was the goal of opening this thing?
And then also we need to know what was returned to us, what kind of information was it that was returned
to us?
And then accordingly, we are going to make changes to the code then.
OK, so then if you do that, so use activity for result, you need to also have a very, very bold
that will take care of the results that we get.
OK, so an activity result, that's the one that we need.
And now what is it that we want to execute here?
Well, I want to check if the result code is that it was successful.
OK, so activity dot result.
OK, this means that we opened the activity, which means we went over to the camera and then we came
back and everything went fine.
And then we check if the request codes.
Well, in this case the request will be our camera.
And here this is the camera interview that we created.
OK, so if the request code is that camera request code and that's that one that we passed when we started
the activity for result here, if that's the case.
Then we want to assign the image that we have taken to the image of you that we have in our UI.
OK, so activity ad update this UI here.
We want to update this image there and we will need a new variable for that.
I'm going to call it thumbnail, which will be of type bitmap.
So I need to import bitmap here.
Import.
And then we can get the data.
That was sent to us, so he had this intent data, and now, again, this is something we can use data
let so this code will only be executed if data is not empty and then we get the extras from that data.
And specifically.
Well, the thing is, extras can also be or also nullable.
So we get the data there.
That's the name of it has a bitmap.
Extras can be inaudible as well, so let me add extras here.
So now we're checking the extras as well as the data.
And at this point, we can add those exclamation marks because we know that it will not be empty.
OK, so data extras, exclamation points, two of them.
And then we get the data as a bitmap and now we can use our binding object to set the image.
So I've add well, that was I've not had but this image that was the one and we said the image a bitmap
to be our thumbnail, that we create it.
So this variable here.
So now if we execute that, let me run it.
Then we will see that we can now, in fact, take an image in our own application.
So this was the final result that we wanted to achieve.
So this is the one that we're building right now, our favorite dish here.
Let's go over.
Let's take an image.
Go to the camera, you see the camera opens up.
Now, let me take another image, this one from the TV screen taken and received, and there we are.
So now our image has been stored inside of this or displayed inside of this image of you.
Now, there's one thing that I want to change as well, because this little icon here is now not representing
what we actually do.
So we want to now edit an image instead of.
Add an image, OK, so what we can do is.
Quickly add a new vector image, so here I'm going to create a new vector asset and here I'm going to
search for PIN.
That didn't do it then?
Well, actually, I'm searching for the pen part.
And here you can search for.
It's this one here, so I'm going to select that and the color that I'm going to use is going to be
white.
So six FS is going to be the trick or is going to do the trick for us.
And I'm going to call this vector added.
All right, so let's click next and finish.
Now we have a new vector file and what we can now do is we can now replace this image here with the
added image.
OK, so I'm just going to go ahead.
And once it was set, well, actually, we can do it directly in here and binding that I've had this
image search image and here it's not going to be a bitmap, but it's going to be a drawable which we
get from our resources folder.
OK, you could have also used image resource, but image drawable is going to do the trick for us.
And here we need to use that context, combat context.
Comput.
Get drawable and then what is destroyable what we need to pass the context as well as.
Our Dods drawable thought I see vector headed.
OK, let me put that into two lines so it's a little more readable.
So there we are.
So this is going to make sure that now the image will also be changed once we have selected an image.
OK, so let's test that and let's see if this is actually going to work.
And if that's the case, then we can move over to the next video.
OK.
So let's see.
Go over here, select an image, or take a camp or open up the camera, take an image story there and
you can see now the icon has been changed.
Now it's the edit icon, which basically means, OK, now we can just select a different image and I'm
just going to move around.
Well, I prefer the dog.
So let's take an image of the dog here.
All right.
And there we are, our dog is there.
All right, so that's it for this video.
See you in the next one.