1
00:00:00,840 --> 00:00:06,750
Welcome back in this video, we're going to add the permission for the external stories, so we want

2
00:00:06,750 --> 00:00:11,580
to read the external storage once we click on this button, this gallery button and then we want to

3
00:00:11,580 --> 00:00:15,090
select an item or an image from the gallery.

4
00:00:15,270 --> 00:00:19,080
And in order to do that, we of course need to, first of all, implement all of the permission stuff

5
00:00:19,320 --> 00:00:25,170
that you have seen in the demo, but wait in order to follow the exact same structure that I'm using

6
00:00:25,170 --> 00:00:25,390
here.

7
00:00:25,410 --> 00:00:29,730
It's important that you are using API version 31.

8
00:00:30,180 --> 00:00:37,350
OK, so that you're not using an older one because otherwise the activity result launcher will not work.

9
00:00:37,590 --> 00:00:42,930
So before you try to implement this yourself, which I would highly recommend, let's quickly look at

10
00:00:43,320 --> 00:00:46,080
the settings that I have in my build Gradle.

11
00:00:46,080 --> 00:00:52,700
So the dependencies that I'm using are the following CTCs 1.6 ABC.com 1.3 one.

12
00:00:53,670 --> 00:01:01,390
Then the material design one for one constraint layer two on one, the J Unit one one three and the

13
00:01:01,710 --> 00:01:04,140
entire press corps version three four zero.

14
00:01:04,560 --> 00:01:10,200
OK, so if you want to have the exact same code working for you, that's going to be important.

15
00:01:10,200 --> 00:01:13,350
And then the compile SDK version should be 31.

16
00:01:13,590 --> 00:01:18,840
The target SDK version should also be thirty one, and the minimum SDK version should be twenty one.

17
00:01:19,200 --> 00:01:23,250
All right, so I highly recommend that you try to build it this way.

18
00:01:23,490 --> 00:01:27,340
I know at this point I would highly recommend that you try to build it yourself.

19
00:01:27,360 --> 00:01:31,740
So here, of course, you need to add the permission that you want to use, and this will be the last

20
00:01:31,740 --> 00:01:36,510
hint that I'm going to give you all the last code that I want to give you before you try it yourself.

21
00:01:36,750 --> 00:01:40,770
And the permission that we're going to need is to read external storage permission.

22
00:01:41,250 --> 00:01:47,760
So at this point, I would recommend that you try to implement the code that we have built in our demo

23
00:01:48,120 --> 00:01:50,880
and the permissions demo.

24
00:01:51,030 --> 00:01:56,400
OK, so try that post a video and then once you're done, you can of course, check out how I did it

25
00:01:56,670 --> 00:01:58,350
and otherwise you can skip that as well.

26
00:01:58,950 --> 00:02:01,290
So good luck with that, and I hope you try it now.

27
00:02:01,890 --> 00:02:02,730
OK, so.

28
00:02:03,570 --> 00:02:08,460
And the main activity, the first thing that I'm going to add is going to be to show this little alert

29
00:02:08,460 --> 00:02:09,030
dialog.

30
00:02:09,120 --> 00:02:14,850
OK, so I'm going to just call it show Russia-NATO dialog, as we have done in the demo.

31
00:02:15,150 --> 00:02:18,270
So if you're wondering what all of this stuff is, where it is coming from?

32
00:02:18,540 --> 00:02:21,270
Well, it just comes from the demo.

33
00:02:21,450 --> 00:02:23,550
OK, so I added it to code there already.

34
00:02:23,850 --> 00:02:28,230
Why should I have to write everything from scratch if I already had built it in the past?

35
00:02:28,230 --> 00:02:34,320
So that's something you can really do when you build your project, really try to reuse as much code

36
00:02:34,320 --> 00:02:36,350
from other projects where it makes sense.

37
00:02:36,360 --> 00:02:41,400
So he had really make sense because this is just a general functionality that I want to use, and that

38
00:02:41,400 --> 00:02:49,590
is just to display an alert dialog with a builder and then set the message that I can pass, as well

39
00:02:49,590 --> 00:02:52,290
as the title that I can pass to this method.

40
00:02:52,860 --> 00:02:58,770
OK, so now we have this private function, and now let's go ahead and go over to the top of our activity

41
00:02:58,770 --> 00:03:02,610
here, where we need to set up our activity launcher.

42
00:03:02,910 --> 00:03:11,640
So I'm going to call this one request permission and it will be of type activity result launcher.

43
00:03:11,640 --> 00:03:13,320
So activity.

44
00:03:15,020 --> 00:03:16,900
Result launcher.

45
00:03:18,640 --> 00:03:24,550
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

46
00:03:24,550 --> 00:03:30,010
strengths because an array of strings, actually because I want to be able to then later on, ask for

47
00:03:30,010 --> 00:03:32,710
multiple permissions and requests for multiple permission.

48
00:03:32,710 --> 00:03:35,080
So I'm going to set it up this way straight away.

49
00:03:35,470 --> 00:03:37,270
And you can see that here.

50
00:03:37,310 --> 00:03:38,710
This import has been added.

51
00:03:38,950 --> 00:03:46,030
And this only works if you have a certain built version here because I tried it to run it on 29 on the

52
00:03:46,030 --> 00:03:52,380
compile version 29 and with my older dependencies, and it just didn't work.

53
00:03:52,390 --> 00:03:57,610
So really, make sure that you have the versions that I'm using and otherwise this activity result launcher

54
00:03:57,610 --> 00:03:58,150
wouldn't exist.

55
00:03:58,150 --> 00:04:00,940
So this is the latest approach to that.

56
00:04:01,510 --> 00:04:07,780
Before that, there was a different approach, which is also why I'm updating this video to the latest

57
00:04:07,780 --> 00:04:09,940
approach of how you can request for permissions.

58
00:04:10,540 --> 00:04:20,500
OK, so here we need to call the register for Activity result method and we need to pass the activity

59
00:04:20,500 --> 00:04:21,670
result contracts.

60
00:04:22,029 --> 00:04:29,380
So activity result contracts with multiple permissions that we are requesting.

61
00:04:30,670 --> 00:04:33,610
OK, so here opening bracket and like.

62
00:04:33,610 --> 00:04:36,940
So and then we get the permissions here.

63
00:04:37,720 --> 00:04:42,520
So here we get the permissions, which means we can use a lambda here.

64
00:04:42,530 --> 00:04:49,210
I'm going to call this one permissions like so and we can now get the permissions entries.

65
00:04:49,510 --> 00:04:52,840
So permissions that I call the permission.

66
00:04:52,960 --> 00:04:53,710
It's permissions.

67
00:04:53,710 --> 00:04:59,590
It's a plural permissions entries thought for each.

68
00:04:59,920 --> 00:05:06,100
So here I'm using for each loop to go through all of the individual permissions that were asked for.

69
00:05:06,730 --> 00:05:10,510
And accordingly, we need to get the permission name.

70
00:05:10,810 --> 00:05:17,150
I'm going to get it like so permission name will be the ID dot key.

71
00:05:17,200 --> 00:05:20,080
So here this mutable map, there's a great part here.

72
00:05:20,080 --> 00:05:26,890
You see, it's a mutable map, mutable entry, and the key is the name of the permission and the value

73
00:05:26,890 --> 00:05:29,260
is whether the permission was granted or not.

74
00:05:29,560 --> 00:05:36,100
So here I'm going to just create a variable called is granted and it will be the value of this mutable

75
00:05:36,100 --> 00:05:36,400
map.

76
00:05:36,400 --> 00:05:37,270
Mutable entry.

77
00:05:38,080 --> 00:05:46,430
OK, now we can just check if is granted, then we can toast the message and we can say permission granted.

78
00:05:46,450 --> 00:05:49,180
You can now read the storage files.

79
00:05:49,270 --> 00:05:51,220
OK, so I'm just going to use this toast here.

80
00:05:51,790 --> 00:05:52,450
Very simple.

81
00:05:52,900 --> 00:05:58,360
And then in the last block of this is granted, I will know that I don't have the permission.

82
00:05:58,990 --> 00:06:05,710
And because I'm going to use multiple permissions in the future, I can just go ahead and directly ask

83
00:06:05,710 --> 00:06:13,030
for what kind of permission name was given because in my case, I know that I'm asking for the permission,

84
00:06:14,050 --> 00:06:17,230
but read external storage and here.

85
00:06:18,240 --> 00:06:23,580
Well, if you use the money first, make sure that you're using the right import as the ID automatically

86
00:06:23,580 --> 00:06:24,960
gives you the Java import.

87
00:06:24,990 --> 00:06:26,310
That's not the one that I need.

88
00:06:26,310 --> 00:06:28,520
I need to use the Android import.

89
00:06:28,530 --> 00:06:36,060
So here use this one, manifest Android and then you see that it will be added all the way at the top

90
00:06:36,060 --> 00:06:36,290
here.

91
00:06:36,300 --> 00:06:37,350
Android Manifest.

92
00:06:37,860 --> 00:06:45,390
OK, so now this has the permissions and the one that I'm going to use is read external storage like

93
00:06:45,390 --> 00:06:48,120
so OK and this now.

94
00:06:49,400 --> 00:06:55,580
Well, in this case, I'm just going to display a toast again for now to saying, oops, you just denied

95
00:06:55,580 --> 00:06:56,180
the permission.

96
00:06:56,390 --> 00:06:58,190
So if the permission isn't granted?

97
00:06:59,250 --> 00:06:59,670
Then.

98
00:07:01,060 --> 00:07:02,080
It will be not granted.

99
00:07:02,440 --> 00:07:08,530
And what checking if it's not granted for that particular permission because, as I said, we can then

100
00:07:08,530 --> 00:07:11,560
later on add more permission requests here as well.

101
00:07:12,520 --> 00:07:12,910
So.

102
00:07:13,910 --> 00:07:18,350
This its value, by the way, is going to be of type Boolean, you can see that here.

103
00:07:18,420 --> 00:07:23,480
So the permission name will be of type, string and it value here will be of type Boolean.

104
00:07:23,840 --> 00:07:28,850
OK, so that's why I can just check if is granted OK, because it will be either true or false.

105
00:07:29,570 --> 00:07:29,990
All right.

106
00:07:30,350 --> 00:07:39,230
So that is how we set up our request permission variable that we can now use inside of our own create

107
00:07:39,500 --> 00:07:39,920
method.

108
00:07:40,370 --> 00:07:46,310
And now, in order to make it work, we of course, need to set up the second image button.

109
00:07:46,730 --> 00:07:50,990
So in the activity main example, we have those two buttons, right?

110
00:07:50,990 --> 00:07:56,510
So we have multiple buttons, but then we have this gallery button, which I called Ibey Gallery, right?

111
00:07:56,750 --> 00:08:01,220
So we need to use a fine view by ID here in order to set up this button.

112
00:08:01,430 --> 00:08:03,200
So I'm just going to set it up here.

113
00:08:03,590 --> 00:08:11,420
I'm going to call this one image button gallery, which will be of type image button like.

114
00:08:11,420 --> 00:08:17,090
So and I'm going to find the view by its ID card out ID, and it was I.B. Gallery.

115
00:08:17,600 --> 00:08:23,000
So now I can use this Iby gallery in order to set the click listener.

116
00:08:23,570 --> 00:08:29,000
And now do the request for permission right here, because that's where we need to do it right.

117
00:08:29,540 --> 00:08:29,900
So.

118
00:08:30,910 --> 00:08:38,409
I could, of course, add all of the request storage permission in here, but then the on create method

119
00:08:38,409 --> 00:08:45,220
would be super large and we can avoid that by creating an extra method that will take care just off

120
00:08:45,520 --> 00:08:49,050
the request storage permission functionality.

121
00:08:49,420 --> 00:08:51,130
So let's create a new method here.

122
00:08:51,820 --> 00:08:55,300
Private fund request storage.

123
00:08:56,420 --> 00:09:05,000
Permission like so and then let's call this inside of our one click listener here.

124
00:09:05,180 --> 00:09:09,380
So once we click on the gallery, we want to request the storage permission currently.

125
00:09:10,550 --> 00:09:12,140
There is, of course, nothing happening here.

126
00:09:12,560 --> 00:09:21,260
But of course, we need to check if we should show request permission rationale, as we have seen in

127
00:09:21,260 --> 00:09:22,790
the demo activity.

128
00:09:23,150 --> 00:09:25,860
Combat is what I'm going to use here.

129
00:09:25,880 --> 00:09:29,300
So activity at DOD should show request permission rationale.

130
00:09:29,540 --> 00:09:37,430
I think this is the longest name for a method that I have used in Android development, not build by

131
00:09:37,430 --> 00:09:37,880
myself.

132
00:09:38,000 --> 00:09:40,250
So not written by myself, but it's really descriptive.

133
00:09:40,250 --> 00:09:45,650
So it really says what it's about and you need to pass in the activity, as well as the permission string

134
00:09:45,650 --> 00:09:46,550
that you want to use.

135
00:09:46,760 --> 00:09:50,090
And the permission is something that we can get directly from the manifest, right?

136
00:09:50,390 --> 00:09:53,750
So the permission string, which just means the name of the permission.

137
00:09:53,750 --> 00:10:00,080
So external storage, because if you look at it, the external storage internal name is this variable

138
00:10:00,080 --> 00:10:04,880
name here, which is this string here and which is, by the way, the same string as we have here.

139
00:10:05,270 --> 00:10:08,490
So that's basically the permission that we are checking.

140
00:10:08,510 --> 00:10:11,690
So is should we show the request permission rationale?

141
00:10:12,260 --> 00:10:14,330
And if so, well, then let's just do it.

142
00:10:14,810 --> 00:10:15,990
And how can we do it?

143
00:10:16,070 --> 00:10:20,360
Well, I mean, first of all, clean this up a little bit.

144
00:10:20,540 --> 00:10:22,760
So this can be going to the next line.

145
00:10:22,790 --> 00:10:23,270
Like so?

146
00:10:23,800 --> 00:10:24,140
So.

147
00:10:25,790 --> 00:10:32,030
If we need to show it, then show it and the rational dialogue will be here, kids drawing app will

148
00:10:32,030 --> 00:10:38,060
be the title and the message will be kids growing up needs to access your external storage.

149
00:10:38,810 --> 00:10:39,060
Okay.

150
00:10:39,080 --> 00:10:43,730
Of course, we could be more descriptive saying otherwise you cannot assign a background image.

151
00:10:44,060 --> 00:10:50,990
So that's something you could right here for the user to know why you should or she should give us access

152
00:10:50,990 --> 00:10:55,780
to this feature because otherwise the user might think, Hey, what does he want to do with my files?

153
00:10:55,790 --> 00:11:01,130
He doesn't have to have access or this app shouldn't have access to my files, which is totally understandable.

154
00:11:01,580 --> 00:11:05,270
So if we should not show request permission rationale.

155
00:11:05,510 --> 00:11:16,490
So in the alt's blog, then we can request for permission and we're going to launch an array of permissions.

156
00:11:16,760 --> 00:11:22,220
And in this case, the array will just be for now having won permission entry.

157
00:11:22,610 --> 00:11:24,980
So read external storage.

158
00:11:25,370 --> 00:11:27,370
We're going to add more later on.

159
00:11:27,380 --> 00:11:32,180
So for example, if you want to start writing to the external storage, once we want to store the images,

160
00:11:32,480 --> 00:11:34,070
that's where we would then add it.

161
00:11:34,270 --> 00:11:44,510
OK, so here you can add a to do at writing external storage permission.

162
00:11:45,590 --> 00:11:48,680
OK, so we just know, OK, well, that's why we will need to add it later on.

163
00:11:48,980 --> 00:11:52,280
But accordingly, we would have to add it in the Android Manifest as well.

164
00:11:52,310 --> 00:11:53,510
We're going to see that later on.

165
00:11:54,200 --> 00:11:54,650
OK.

166
00:11:54,830 --> 00:11:57,980
So at this point, we should be done.

167
00:11:58,070 --> 00:11:59,150
Let's test it.

168
00:11:59,160 --> 00:12:02,270
I don't need this import image because I just misspelled something there.

169
00:12:02,660 --> 00:12:09,440
Let's try it out and see if we're going to request for permission once we click on our gallery button.

170
00:12:11,120 --> 00:12:11,600
Image problem.

171
00:12:12,630 --> 00:12:16,350
By the way, the next part that I'm going to show you so the next couple of minutes where I'm showing

172
00:12:16,350 --> 00:12:21,960
you how I make changes to my Gradle files, as well as to my Android manifest.

173
00:12:22,290 --> 00:12:28,270
They will not be necessary if you are using the Arctic Fox version that I was recommending.

174
00:12:28,350 --> 00:12:31,440
So the Arctic Fox version 2020. three.

175
00:12:31,890 --> 00:12:35,850
And if that's the case, then you will not have to make those changes.

176
00:12:35,850 --> 00:12:40,860
So what I would recommend is that you just run the application and test it on your emulator or device.

177
00:12:40,950 --> 00:12:47,160
And if it works, you can just ignore the next two minutes of video and then just move on with the course.

178
00:12:47,640 --> 00:12:51,390
So what I did is I used my old project to build up on it.

179
00:12:51,630 --> 00:12:54,620
And according to, of course, the versions were different.

180
00:12:54,630 --> 00:13:01,440
So if you want to use the version that I'm using here, I'm using the current blend version 1.5 30 and

181
00:13:01,440 --> 00:13:03,880
here in that case, you don't need to set up JS Center.

182
00:13:04,110 --> 00:13:12,540
You can use the MAVEN Central here, as well as I'm using the the Gradle version seven zero two.

183
00:13:12,540 --> 00:13:14,510
If you do that, you need to sync your file.

184
00:13:14,520 --> 00:13:18,420
And then once you did that and think that you might need to migrate.

185
00:13:18,420 --> 00:13:24,240
But once you do that in the Android Manifest, you need to add Android exported setting.

186
00:13:24,540 --> 00:13:31,440
So here Android exported must be set to true in order for you to be able to run it with this particular

187
00:13:31,440 --> 00:13:31,800
version.

188
00:13:32,070 --> 00:13:39,990
If you, however, started building this application using directly Android API 31, you should not

189
00:13:39,990 --> 00:13:44,070
run into these problems because these settings should have been set up in the same way.

190
00:13:44,370 --> 00:13:45,540
The same goes for the version.

191
00:13:45,540 --> 00:13:48,360
If they're close enough, then you shouldn't have to make changes.

192
00:13:48,720 --> 00:13:53,380
But other than that, that's basically going to be the code that we need to use.

193
00:13:53,400 --> 00:13:56,940
So let's go ahead and actually test our application.

194
00:13:57,330 --> 00:14:04,410
So once we click on the button, you can now see that if we don't allow, it will say, Oops, you just

195
00:14:04,410 --> 00:14:06,780
denied, even though we didn't denied actively.

196
00:14:06,780 --> 00:14:11,880
But by not clicking on it, we are, so to speak, denying or not giving access.

197
00:14:11,880 --> 00:14:13,260
Or at least we are not giving access.

198
00:14:13,260 --> 00:14:14,430
That's the whole point here, right?

199
00:14:14,730 --> 00:14:18,480
So now if we allow it, you can see permission granted and you can read the storage files.

200
00:14:18,750 --> 00:14:26,220
And if I click it once again, you see, I just get this detail here, but I don't well do any other

201
00:14:26,220 --> 00:14:27,630
features or activities.

202
00:14:28,020 --> 00:14:30,270
And the thing is what we want to do.

203
00:14:30,280 --> 00:14:37,080
And instead of displaying the toast here, what we want to do is we want to actually then run the code

204
00:14:37,080 --> 00:14:42,900
that we want to execute, which will open a new activity for result, which will then take an image

205
00:14:43,170 --> 00:14:48,030
from the gallery, which we can select and then put it into our application.

206
00:14:48,420 --> 00:14:51,150
So that's what we're going to set up in the next few videos.

207
00:14:51,480 --> 00:14:53,100
But for now, that is it.

208
00:14:53,280 --> 00:14:54,750
So see you in the next video.

