Welcome back in this video, we're going to add the permission for the external stories, so we want
to read the external storage once we click on this button, this gallery button and then we want to
select an item or an image from the gallery.
And in order to do that, we of course need to, first of all, implement all of the permission stuff
that you have seen in the demo, but wait in order to follow the exact same structure that I'm using
here.
It's important that you are using API version 31.
OK, so that you're not using an older one because otherwise the activity result launcher will not work.
So before you try to implement this yourself, which I would highly recommend, let's quickly look at
the settings that I have in my build Gradle.
So the dependencies that I'm using are the following CTCs 1.6 ABC.com 1.3 one.
Then the material design one for one constraint layer two on one, the J Unit one one three and the
entire press corps version three four zero.
OK, so if you want to have the exact same code working for you, that's going to be important.
And then the compile SDK version should be 31.
The target SDK version should also be thirty one, and the minimum SDK version should be twenty one.
All right, so I highly recommend that you try to build it this way.
I know at this point I would highly recommend that you try to build it yourself.
So here, of course, you need to add the permission that you want to use, and this will be the last
hint that I'm going to give you all the last code that I want to give you before you try it yourself.
And the permission that we're going to need is to read external storage permission.
So at this point, I would recommend that you try to implement the code that we have built in our demo
and the permissions demo.
OK, so try that post a video and then once you're done, you can of course, check out how I did it
and otherwise you can skip that as well.
So good luck with that, and I hope you try it now.
OK, so.
And the main activity, the first thing that I'm going to add is going to be to show this little alert
dialog.
OK, so I'm going to just call it show Russia-NATO dialog, as we have done in the demo.
So if you're wondering what all of this stuff is, where it is coming from?
Well, it just comes from the demo.
OK, so I added it to code there already.
Why should I have to write everything from scratch if I already had built it in the past?
So that's something you can really do when you build your project, really try to reuse as much code
from other projects where it makes sense.
So he had really make sense because this is just a general functionality that I want to use, and that
is just to display an alert dialog with a builder and then set the message that I can pass, as well
as the title that I can pass to this method.
OK, so now we have this private function, and now let's go ahead and go over to the top of our activity
here, where we need to set up our activity launcher.
So I'm going to call this one request permission and it will be of type activity result launcher.
So activity.
Result launcher.
This one here, and I need to define what type it will be off, and I'm going to say it's going to be
strengths because an array of strings, actually because I want to be able to then later on, ask for
multiple permissions and requests for multiple permission.
So I'm going to set it up this way straight away.
And you can see that here.
This import has been added.
And this only works if you have a certain built version here because I tried it to run it on 29 on the
compile version 29 and with my older dependencies, and it just didn't work.
So really, make sure that you have the versions that I'm using and otherwise this activity result launcher
wouldn't exist.
So this is the latest approach to that.
Before that, there was a different approach, which is also why I'm updating this video to the latest
approach of how you can request for permissions.
OK, so here we need to call the register for Activity result method and we need to pass the activity
result contracts.
So activity result contracts with multiple permissions that we are requesting.
OK, so here opening bracket and like.
So and then we get the permissions here.
So here we get the permissions, which means we can use a lambda here.
I'm going to call this one permissions like so and we can now get the permissions entries.
So permissions that I call the permission.
It's permissions.
It's a plural permissions entries thought for each.
So here I'm using for each loop to go through all of the individual permissions that were asked for.
And accordingly, we need to get the permission name.
I'm going to get it like so permission name will be the ID dot key.
So here this mutable map, there's a great part here.
You see, it's a mutable map, mutable entry, and the key is the name of the permission and the value
is whether the permission was granted or not.
So here I'm going to just create a variable called is granted and it will be the value of this mutable
map.
Mutable entry.
OK, now we can just check if is granted, then we can toast the message and we can say permission granted.
You can now read the storage files.
OK, so I'm just going to use this toast here.
Very simple.
And then in the last block of this is granted, I will know that I don't have the permission.
And because I'm going to use multiple permissions in the future, I can just go ahead and directly ask
for what kind of permission name was given because in my case, I know that I'm asking for the permission,
but read external storage and here.
Well, if you use the money first, make sure that you're using the right import as the ID automatically
gives you the Java import.
That's not the one that I need.
I need to use the Android import.
So here use this one, manifest Android and then you see that it will be added all the way at the top
here.
Android Manifest.
OK, so now this has the permissions and the one that I'm going to use is read external storage like
so OK and this now.
Well, in this case, I'm just going to display a toast again for now to saying, oops, you just denied
the permission.
So if the permission isn't granted?
Then.
It will be not granted.
And what checking if it's not granted for that particular permission because, as I said, we can then
later on add more permission requests here as well.
So.
This its value, by the way, is going to be of type Boolean, you can see that here.
So the permission name will be of type, string and it value here will be of type Boolean.
OK, so that's why I can just check if is granted OK, because it will be either true or false.
All right.
So that is how we set up our request permission variable that we can now use inside of our own create
method.
And now, in order to make it work, we of course, need to set up the second image button.
So in the activity main example, we have those two buttons, right?
So we have multiple buttons, but then we have this gallery button, which I called Ibey Gallery, right?
So we need to use a fine view by ID here in order to set up this button.
So I'm just going to set it up here.
I'm going to call this one image button gallery, which will be of type image button like.
So and I'm going to find the view by its ID card out ID, and it was I.B. Gallery.
So now I can use this Iby gallery in order to set the click listener.
And now do the request for permission right here, because that's where we need to do it right.
So.
I could, of course, add all of the request storage permission in here, but then the on create method
would be super large and we can avoid that by creating an extra method that will take care just off
the request storage permission functionality.
So let's create a new method here.
Private fund request storage.
Permission like so and then let's call this inside of our one click listener here.
So once we click on the gallery, we want to request the storage permission currently.
There is, of course, nothing happening here.
But of course, we need to check if we should show request permission rationale, as we have seen in
the demo activity.
Combat is what I'm going to use here.
So activity at DOD should show request permission rationale.
I think this is the longest name for a method that I have used in Android development, not build by
myself.
So not written by myself, but it's really descriptive.
So it really says what it's about and you need to pass in the activity, as well as the permission string
that you want to use.
And the permission is something that we can get directly from the manifest, right?
So the permission string, which just means the name of the permission.
So external storage, because if you look at it, the external storage internal name is this variable
name here, which is this string here and which is, by the way, the same string as we have here.
So that's basically the permission that we are checking.
So is should we show the request permission rationale?
And if so, well, then let's just do it.
And how can we do it?
Well, I mean, first of all, clean this up a little bit.
So this can be going to the next line.
Like so?
So.
If we need to show it, then show it and the rational dialogue will be here, kids drawing app will
be the title and the message will be kids growing up needs to access your external storage.
Okay.
Of course, we could be more descriptive saying otherwise you cannot assign a background image.
So that's something you could right here for the user to know why you should or she should give us access
to this feature because otherwise the user might think, Hey, what does he want to do with my files?
He doesn't have to have access or this app shouldn't have access to my files, which is totally understandable.
So if we should not show request permission rationale.
So in the alt's blog, then we can request for permission and we're going to launch an array of permissions.
And in this case, the array will just be for now having won permission entry.
So read external storage.
We're going to add more later on.
So for example, if you want to start writing to the external storage, once we want to store the images,
that's where we would then add it.
OK, so here you can add a to do at writing external storage permission.
OK, so we just know, OK, well, that's why we will need to add it later on.
But accordingly, we would have to add it in the Android Manifest as well.
We're going to see that later on.
OK.
So at this point, we should be done.
Let's test it.
I don't need this import image because I just misspelled something there.
Let's try it out and see if we're going to request for permission once we click on our gallery button.
Image problem.
By the way, the next part that I'm going to show you so the next couple of minutes where I'm showing
you how I make changes to my Gradle files, as well as to my Android manifest.
They will not be necessary if you are using the Arctic Fox version that I was recommending.
So the Arctic Fox version 2020. three.
And if that's the case, then you will not have to make those changes.
So what I would recommend is that you just run the application and test it on your emulator or device.
And if it works, you can just ignore the next two minutes of video and then just move on with the course.
So what I did is I used my old project to build up on it.
And according to, of course, the versions were different.
So if you want to use the version that I'm using here, I'm using the current blend version 1.5 30 and
here in that case, you don't need to set up JS Center.
You can use the MAVEN Central here, as well as I'm using the the Gradle version seven zero two.
If you do that, you need to sync your file.
And then once you did that and think that you might need to migrate.
But once you do that in the Android Manifest, you need to add Android exported setting.
So here Android exported must be set to true in order for you to be able to run it with this particular
version.
If you, however, started building this application using directly Android API 31, you should not
run into these problems because these settings should have been set up in the same way.
The same goes for the version.
If they're close enough, then you shouldn't have to make changes.
But other than that, that's basically going to be the code that we need to use.
So let's go ahead and actually test our application.
So once we click on the button, you can now see that if we don't allow, it will say, Oops, you just
denied, even though we didn't denied actively.
But by not clicking on it, we are, so to speak, denying or not giving access.
Or at least we are not giving access.
That's the whole point here, right?
So now if we allow it, you can see permission granted and you can read the storage files.
And if I click it once again, you see, I just get this detail here, but I don't well do any other
features or activities.
And the thing is what we want to do.
And instead of displaying the toast here, what we want to do is we want to actually then run the code
that we want to execute, which will open a new activity for result, which will then take an image
from the gallery, which we can select and then put it into our application.
So that's what we're going to set up in the next few videos.
But for now, that is it.
So see you in the next video.