Welcome back and this video, we are going to combine several things that we have touched on in the
last few videos, and we're going to see how we can now go ahead and use the camera in order to select
an image or shoot an image first and then select that image in order to also display it as an image
here.
So pretty much get an image from the camera to our application.
As you can see here now, the image is inside of this little image.
You hereby we're not going to take care of the specifics of how to make the image of you beautiful and
how to make the beautiful, but really just the functionality to start the intent to get the image and
then to get the result of that activity, so to speak, to display this in our images.
Therefore, I have an image of you here.
You can set any settings you want, then a button which allows us to start the content in order to ask
for permissions.
And then, of course, if we have the permissions to start the activity, to display the camera application
in order to then make an image and of course, then take that image and display the idea.
So in order to do that, we need to prepare several things.
So first of all, we need to have the permissions.
Therefore, we can go to our manifest and here we need to use the permission for the camera.
So we're just going to use Android permissions camera.
All right, so this is the only permission that we're going to need in this example, and if you want
to know more about permissions, then definitely check out the video on permissions.
This is not specifically for that or this video here, because it's really just about the camera functionality.
Then we need a companion object in which we store our variables, which are our constants, so to speak.
So here the camera, a permission code will be one and then the camera value will be to.
So one of them will be for the permission code, the other one will be for the intent and activity for
result.
That will make a lot more sense once we get into it.
So first of all, we need, of course, our camera on Click Listener.
So let's set that.
And then the idea here is to, first of all, check if we have permissions.
So that's generally always what we need to do if we want to use the camera.
So check if we have permission.
And which permission are we looking for?
Well, specifically the camera permission and we check if the permission was granted.
So if that's the case, then we want to use this camera functionality.
So here comes in the interesting part.
So we have this intent, which will be an intent that goes to and he wanted to import it real quick,
which goes to the media store that action or it's just going to use this action image, capture functionality.
So we prepared this intent to start our camera, pretty much that's what this does, all right.
And then we can start the activity for result with the intent.
And what shall we open?
Well, we pass in the camera, which is our.
It's variable that we created here.
So this constant here, so we start the activity for result and as you know, when we do that, we need
to pass in the intent that we want to start and then the code.
To be more precise, the request code now, of course, you can call us one request coach if you want
to be super clear about this.
All right.
So here we're passing the request code when starting the activity for a result.
So we're pretty much waiting for a result.
Now, of course, if we don't have permission, then we want to ask for permission.
And I'm just going to use the standard format here, activity, combat request permissions.
And then you need to pass an activity in which you want to request that the array of different permissions
in our case is just one permission, which is a camera permission.
And then we need to pass ENDA permission code as we prepared it up here.
All right, now, of course, we need to do something in order to get the permissions running.
So here on request.
Permissions.
Result.
So this one here.
It's going to be the one that we use in order to.
Execute something.
Once we get a permissions result.
So either the user allowed it or he denied it.
So I check if the permission request code, so request code is equal to the camera permission code.
Because we're talking about emissions right here, here, the request code, if that's the same, then
I want to check if the grant results.
So this one here does a rate that we get here.
As you can see, is an array of integers.
If that is not empty and current results.
At position zero.
His permission granted.
So here.
Package manager, not permission granted.
All right, so if permission is granted, then I want to pretty much do the same thing as we've done
before with the intent.
So here I want to start this activity for result here.
So I'm going to paste that in here as well.
Right.
And otherwise, if we didn't get the grant result.
So here in this health blog.
I just want to write a little toast and say the permission was denied.
So I'm just going to write something like you just denied the permission for camera.
Don't worry, can out in the settings, OK?
So this is the on request permission results, so this is, again, nothing new if you watched the permissions
video.
Now comes the Newport, the interesting part when it comes to the camera, because now we combine what
we have seen with activity for result.
And.
The usage of cameras, because what we get from an activity for result is on activity result.
So this function here is automatically then called once we get the results.
So once the user, for example, selected an image with a camera.
So now we can go ahead and check if the result code.
It's going to be activity result.
OK.
And why we need to import activity and if that's the case, then we can check if the request code is
equal to our camera request code.
So here, camera request Kote.
Because if that's the case, then we can display the data that we are oppressed from an activity result
into our live image, which is and our activity in this image view, which was called IVI image.
So then we can set that image view to be the image that we have just received as the result of an activity
result, which pretty much as a result on start activity for yourself.
OK, so now we can go ahead and hear.
Get the data, and I'm saving that in a thumbnail, which is of type bitmap, and we get that need to
import it real quick, then we get that from data.
That extra us.
So here, Daito.
That was passed has some extras, and from there we want to get that data.
So it's called data as a bitmap so the data can be in many different shapes and forms, but we want
to get it as a bitmap.
Now, the thing is, data is a nullable and extras is a nullable as well.
So we need to focus on Revit here and then we have the thumbnail now we can go ahead and use that.
So our image view.
So I've image set, image bitmap can be our thumbnail because our thumbnail is.
A bitmap and here, of course.
Writing it correctly will be a little better.
So here we set the image of the image for you to be, the thumbnail that we get from the data that was
passed to us from the productivity result.
So pretty much when we press on this button.
Then we get to the camera, so that is.
Here, set on Click Listener, so we click on the button it checks, do we have permission?
If we have permission, which we do, then start a certain Internet media store action image capture,
which is this screen here that it will be started as soon as this start activity result is.
Executed.
OK, so then and waits for the result.
And once I select an image here, I press this button, then it goes back to this activity that we are
at.
So it goes back to our main activity and this one activity result is executed.
So then it checks as the result code of result code, OK, which means that the selection work out.
And then was the request code that got you that activity result, was it the common request code?
So what do we get the camera requests from?
Well, the thing is, we passed it here when we started the activity for results.
So you can see your intent with the camera request code.
And that is, of course, our constant that we have created here.
OK, so we just use it as a concept because we want to use it multiple times throughout the application
or potentially use it throughout the application.
And it's just good practice to store it in a companion object.
And then we can, of course, execute whatever we want to do if the result came back.
Now the thing is the result is of type data, but in this case, the data that we get is actually going
to be a bitmap.
So now we can take that bitmap and store it as a bit more variable and then set our image for you to
be that bitmap.
All right.
And similarly, other selections work as well, or other activity for result options work as well.
So here you can, of course, use other types of intense.
It could be an intent that we will use later on where we start an activity that we don't develop ourselves.
And this intent could go to third party activities as well.
For example, we are going to use places later on, which is from Google, where you can enter a name
of a place and it will display that place on the map.
And we want to get the information from the place, for example, the latitude and longitude, and we
want to get that, of course, in the activity that started the intent.
So we are also going to use the same approach, which is start activity for result.
All right.
So these are the basics behind it.
And of course, we are going to look into that again and again, of course, each time when we use that.
And we're going to use it quite a lot.
So no worries if that was a little bit too much now.
And see you in the next video.