Welcome back.
In the last year, we added this button, or in the last week where we worked on this project, we added
this little button here at the bottom, which is this gallery icon.
And I want this to open up the gallery in order for me to select an image which shall replace the background
image that we have here.
In order to do that, we, of course, need to get the permission to read something from the external
storage.
And in order to do so, we need to go to our Android manifest file, which is inside of the manifests
folder.
So Android manifest XML and in here I need to use permission.
So here the permission that I want to use is read external storage.
OK, so you would have to type that in in order to get there.
And then I also want to write the external storage or to the external storage to be more precise.
So here.
Right, external storage.
What that will allow me to do is to let Iran also export the image to the storage.
So once you want to save the image, you don't need to create a screenshot of it, but you can actually
export it later on.
That's a feature that we are going to add later.
The next thing we need to do is to go to our main activity, because that's where we are going to ask
for the permission, because that's activity in which we are going to require the permission.
OK, so what we, of course, can do directly is to create a companion object.
And I'm going to do that inside of the main activity, just inside of it, but not inside of a method.
And I'm going to call this one companion object.
Which will be storing all of our static variables and our constants, so private const VUL storage,
underscore permission, underscore code and I'm going to assign one.
So you might recall we had to create this static variable in order to double check if the permission
that we are requesting is actually the one that we are looking at.
OK, so that's the storage permission code and now we can use that.
So let's go over and create a new function here.
Private fun request, storage permission.
So I'm going to call it because this function is going to take care of the requesting of the storage
permission.
I don't want to do that in the uncreate method directly.
I want to do it outside.
So I'm going to create this method which will take care of that.
So here I will check if I should request the permission or not.
So if activity combat
thought should show request permission.
Rusciano, and then I need to pass in the activity that we're looking at so this and an array of permissions,
so I'm going to use a ray of manifest thought, permission, dot read, underscore external, underscore
storage.
And here, of course, it helps if we import the manifest and the entry points.
So there are two options here, as you can see, and I'm using the Android one and then I can use this
read external permission.
So let's see if I wrote that correctly.
Read on the score, external storage, that's how it's called.
And then I also want to ask for the for the writing one.
OK, so manifest that permission Dot.
Right.
External storage, OK, so I'm going to put that in the next line so it's a little more readable, but
that by itself is not enough because this array often needs to be of top string.
So I'm going to cast it to be a string.
OK, so now what should I do if that happens?
So what should I do if I need to give a rationale?
Well, that's what I can add inside of these curly brackets.
OK, so not enough space here, but that's what I would need to write here.
Now it's a little better.
In those brackets, so he could write a toast or something like that to inform the user that you actually
need to have storage permission so you could add something like need permission to add a background.
OK, so that's just what I'm going to do here.
Of course, you could create a much more sophisticated code here.
And then, of course, I also want to actually request the permission.
So this is just a code that if you want to have a rationale, so if the user is denying, for example,
then we would let them know.
All right.
So now let's actually request the permission.
So here we use activity, compare that request permission and it's plural because we can request multiple
permissions.
So it's an array of.
Manifest and actually it's the same as we had here.
So let's just copy this whole thing here.
All right, and then we need to pass in our storage permission code, so now you see that there is a
little difference between those two approaches or should request permission.
Russia now is returning and bullion and then that goes all the way up here.
And then also when using it, you see this array of has to be converted into a string.
But when we request the permission itself, we don't need to do that.
OK, so that's just how the code is developed.
And then storage permission code is what we also need to pass in here.
But that's something that we don't need to pass.
And when calling this method OK, we in the example will use this kind of approach that we have here.
But we didn't create an additional method for this.
This time we're doing it in order to keep everything clean and tidy in our code.
All right.
Now we need another function which will take care of what should happen once the user gives us permission
or he doesn't.
So we use this on request permission result.
Functio and in here.
We need to check if the request code was, in fact, our storage permission code.
And if that's the case, then we want to see if we got access or not.
So if the grant result is not empty and the grant results.
At the position that we're currently looking at, so at position zero is package manager thought permission
granted.
OK, so let me put that in another line so it's a little more readable, so this on request permission
resoled.
As you can see, it has a bunch of variables here, which are part of the definitions of the parameters
here, which is the request code, the permissions, and then the current result, which is an array
of integers, OK, and the permissions are an array of strings.
So pretty much what we had here.
Array of strengths.
And then what I'm doing here is I'm checking if the request code, so this one here does integer is
the same as the request code that I had for my storage permission, because that's the request that
I started here.
So here I start to request for permission and here I'm getting the result of it.
So whatever I passed in here will be sent over as a request code in our case to request code as to storage
permission code, and then I can check if to grant results.
So the result of was is granted or not so that the user allow or did you press and deny it's going to
be the result in here.
So it's going to be in grant results and to position zero, which is the first entry, and then we check
if it was granted.
So if the user press allow and if he did that, then what I'm going to do is I'm just going to show
the little text here.
So I'm just going to add this tost permission granted.
Now you can read the storage files.
OK, so that's just the tools that I'm adding in here.
You can, of course, write your own toast or.
Yeah, use something else if you want or execute some other code.
But this is just for the user to indicate that he gave us the rights to.
Used to storage and now what should happen if we don't get the rights?
Well, then I'm just going to write another toast and it's going to look something like this.
Oops, you just denied the permission.
So here we could also add to, oh, please give us the permission because we need it for the application
to run correctly, for example.
All right, now we will know if we have the rights or not.
OK, so we could go ahead and say, all right, let's store that in a variable which will be permanently
saved, and then we can constantly use the feature when we need it.
But the problem here is that if the user changes to permission in the settings, we would never know
because it's not inside of the application itself.
So it wouldn't happen inside of the application.
So we would never change his consent and then suddenly we wouldn't have access to it anymore.
So what we need to do is or what we can do is a little helper function, so to speak, which we'll figure
out if we have access.
Is storage allowed or not.
So that's going to be a function which will return a boolean and it will just let us know if we have
access or not.
So if we have access, then we can go ahead and use it and otherwise.
Well, we can't.
So, of course, it needs to return something, so in order to get what we want, we need to store it
in a variable.
And I'm going to use results here and I'm going to use context compote to check the self permission.
So did we get access to something or not?
So check the subcommission.
And inside of this context, we need to put this into context and then you need to pass in the actual
permission that you are looking at.
So I'm looking at the permission here.
First of all, read external storage.
OK, so whatever the result of this is will be stored in the result, terrible, and now we can go ahead
and return a result was true or not.
So the problem is result is not actually going to be of type boolean, but it's going to be of another
type.
So we can go ahead and say if result is equal to package manager, that permission granted.
Then we want to return true and otherwise we will return false.
So dysfunction, it's just going to check if we have storage access or not, with the permission and
if we have it, it will return.
True.
And if we don't, then it will return false.
OK, so that is the storage allowed.
It's a little helper method, which is really helpful because now we can just check this thing or call
this function to see if we have access or not and only execute the code that we want to execute on,
for example, the storage if we actually have access rights.
All right.
Now let's go to our button here that we want to implement.
So that's an image button on the score gallery set on Click Listener.
So we want to run something once we click on the I.B. Gallery, which is our image button here.
OK, this gallery button.
So in order to get there, we need to check if we have permission.
So what do we want to do?
Well, we want to actually get something from the gallery, so we want to use an image from the gallery
to put it as the background.
So we need to ask for the permission.
And if we already have permission, then we don't need to ask for permission.
So we check first.
Do we already have permission?
So is storage allowed?
And if we already have permission, then we want to run our code.
So run our code to.
Get the image from the gallery, OK, so that is something that is cool.
Now we can do whatever should happen if the permission wasn't given.
So I'm just going to check here for storage permission.
So request storage permission.
So now you can see why we created this additional function here.
Is storage allowed and this request storage permission?
Because those two functions are really outside of our uncreate.
So the uncreate method is kept short and sweet and at the same time we can very easily adjust it if
we need it.
So we have this function request storage permission, which takes care of requesting the storage permission
and we have the other function which checks if we already have storage permission or not.
This one here is research allowed.
So if we already have storage, cool, then let's go for it.
Let's do something so we don't need to have all of this code that we have in these functions inside
of our own click listener.
And that's why I chose this approach.
So it's a lot cleaner this way.
Now, of course, this code that we had here, this on request permission result is not something that
we would really need.
It's just code that I added in order for you to see something.
So in order to see when we give permission or not.
OK, so let's test it.
Let's see if this is actually going to work.
And there we are.
So let's press the button and see that we are asked to give permission, so I'm going to deny it first,
OK?
Oops, you just denied the permission.
OK, that code red.
And now we can allow it.
OK, so permission granted.
Now you can read the start file.
Just click on it again and you could see nothing happens because of course we didn't implement the code
yet that will then select an image and that code is inside of our uncreate.
So here in I.B. Gallery set on Click Listener, this is the code that will be executed each time that
I click on the button now because I already have access.
All right, great.
So now you know how to use it and now we have implemented it.
I mean, you have seen how to use permissions before, but now we have actually used them directly in
our project here.
And yes, you in the next video where we're going to implement the gallery feature.