1
00:00:00,400 --> 00:00:01,220
Welcome back.

2
00:00:01,450 --> 00:00:07,180
In this video, we are going to implement the functionality to add an image here, so to select an image

3
00:00:07,180 --> 00:00:10,430
from our storage and then use it in our profile.

4
00:00:10,450 --> 00:00:14,680
So, for example, I can click on this icon, then it will ask me for permission.

5
00:00:14,680 --> 00:00:20,230
I'm just going to allow it for this case and then I can go ahead and select a photo from the camera.

6
00:00:20,230 --> 00:00:27,910
So let's select our Homer once again and then it will populate the image accordingly, as you can see

7
00:00:27,910 --> 00:00:28,210
here.

8
00:00:29,750 --> 00:00:32,900
All right, so you should already know how to do that, right?

9
00:00:32,960 --> 00:00:34,800
You have seen that multiple times.

10
00:00:35,060 --> 00:00:41,900
The thing is, what we will start with is the Internet connection, because we also want to have Internet

11
00:00:42,530 --> 00:00:43,940
rights, so to speak.

12
00:00:44,130 --> 00:00:48,020
Of course, also storage rights, as we will use that as well.

13
00:00:48,260 --> 00:00:55,010
So let's go over to our Android manifest and use those permissions or add those permissions here, because,

14
00:00:55,010 --> 00:00:56,300
as I said, we will need them.

15
00:00:56,480 --> 00:01:00,590
And then the rest you can try to do pretty much yourself.

16
00:01:00,590 --> 00:01:00,830
Right.

17
00:01:00,860 --> 00:01:04,430
So you know how to do all of the steps that are necessary for this.

18
00:01:04,430 --> 00:01:06,680
So you can just post the video and try to implement that.

19
00:01:06,890 --> 00:01:10,280
That will definitely improve your development skills.

20
00:01:10,290 --> 00:01:11,810
So go ahead and try it.

21
00:01:13,390 --> 00:01:19,890
All right, so I hope you tried it and were successful, so what I'm going to use here is the Internet,

22
00:01:21,490 --> 00:01:23,020
so this is permission and No.

23
00:01:23,020 --> 00:01:23,310
One.

24
00:01:23,710 --> 00:01:27,240
Next, we need to permission for the network state.

25
00:01:27,790 --> 00:01:35,020
So network state, this one here and then the one for external storage.

26
00:01:35,080 --> 00:01:37,660
So external storage.

27
00:01:37,660 --> 00:01:40,800
And for now we're just reading external storage.

28
00:01:40,810 --> 00:01:42,190
We're not going to store anything.

29
00:01:42,880 --> 00:01:43,600
As of now.

30
00:01:43,660 --> 00:01:48,610
If you want to store anything on the external storage, you can of course, also add the right external

31
00:01:48,610 --> 00:01:49,600
storage permission.

32
00:01:49,630 --> 00:01:51,610
But this is going to do it for now.

33
00:01:52,000 --> 00:01:54,580
So now let's go over to our profile activity.

34
00:01:55,500 --> 00:02:01,410
Because here is where the magic will happen, but I just want a little thing is I want to make sure

35
00:02:01,860 --> 00:02:09,030
that my image view has an I.D. So I called this circle image view.

36
00:02:09,030 --> 00:02:11,250
I see more I.V. user image.

37
00:02:11,490 --> 00:02:17,250
Maybe we should make it more precise because this is the profile user image.

38
00:02:17,760 --> 00:02:23,100
OK, so it's going to be unique now because I think we used IV user image somewhere else already.

39
00:02:23,430 --> 00:02:27,390
So it always makes sense to have IDs as unique values.

40
00:02:29,170 --> 00:02:36,430
All right, and as it is with permissions, you maybe recall it makes sense to have constants for the

41
00:02:36,430 --> 00:02:37,170
permission codes.

42
00:02:37,360 --> 00:02:46,810
So let's go ahead and create a companion object which will have the private constant value read storage

43
00:02:47,140 --> 00:02:50,590
permission code, which will be one.

44
00:02:51,370 --> 00:02:56,770
And then because we know we will also need the image request code.

45
00:02:57,040 --> 00:02:58,540
Let's just add that as well.

46
00:02:59,590 --> 00:03:08,290
So pick an image request code and let's assign to value to to it.

47
00:03:10,210 --> 00:03:13,330
All right, so when should anything happen or something happen?

48
00:03:13,660 --> 00:03:18,250
Well, as soon as we click on the button and by the way, here said user data.

49
00:03:18,850 --> 00:03:20,970
So now it's, of course, renamed.

50
00:03:20,980 --> 00:03:24,430
So I've provided user image, just a new name.

51
00:03:25,030 --> 00:03:29,260
And they said user data UI, it's OK in our uncreate.

52
00:03:29,260 --> 00:03:35,170
We can now go ahead and give our image view and on Click Listener.

53
00:03:35,200 --> 00:03:44,860
So I've profile user image set on Click Listener so that we can execute some code in here.

54
00:03:45,210 --> 00:03:49,030
And of course we want to do is we want to check if we have permission already.

55
00:03:49,360 --> 00:03:51,510
So if context compact.

56
00:03:52,750 --> 00:03:57,700
So here context compared check self permission.

57
00:04:00,560 --> 00:04:09,020
And here we ask for the external reading, so here we need to pass in the context and by the way, let's

58
00:04:09,020 --> 00:04:10,380
import context compared.

59
00:04:11,770 --> 00:04:17,019
Just so the area disappears, so we need to pass in the context and then the permission that we want

60
00:04:17,320 --> 00:04:22,870
and we will just get it from manifest permission to read external.

61
00:04:24,750 --> 00:04:27,600
Your external storage.

62
00:04:29,690 --> 00:04:30,200
All right.

63
00:04:30,440 --> 00:04:36,770
And here we also need to import manifest would have been easier if we had imported it before.

64
00:04:37,740 --> 00:04:42,420
Because then it will tell us which one of those is correct or incorrect.

65
00:04:42,870 --> 00:04:45,290
OK, so that is the request that we have.

66
00:04:45,300 --> 00:04:50,850
And then, of course, we want to execute some code in the case that we have this permission.

67
00:04:51,390 --> 00:04:58,410
And we, of course, need to check it, because this is just a check here.

68
00:04:58,650 --> 00:05:00,540
Is it permission granted?

69
00:05:02,240 --> 00:05:03,950
So here, package manager.

70
00:05:05,880 --> 00:05:08,100
That permission granted.

71
00:05:08,910 --> 00:05:12,750
So this is always the structure and I think it makes sense to have it in multiple lines.

72
00:05:13,050 --> 00:05:19,690
So we're saying if this here is equal to package manager, permission granted, OK?

73
00:05:20,160 --> 00:05:26,000
And if that's the case, then what we want to do is we want to show the image chooser.

74
00:05:26,010 --> 00:05:28,290
So we want to select the image pretty much.

75
00:05:29,650 --> 00:05:36,190
Otherwise, so in the case, if we don't have permission and that's what we can implement before we

76
00:05:36,190 --> 00:05:40,480
implement that one, so otherwise we want to ask for permission.

77
00:05:40,900 --> 00:05:45,700
So activity compact's requests, permissions.

78
00:05:47,200 --> 00:05:50,880
And here, permissions, let's import activity combat here as well.

79
00:05:53,310 --> 00:06:02,850
OK, and then in here, we need to first in person the activity and then the array of our permissions

80
00:06:02,850 --> 00:06:04,040
that we want to ask for.

81
00:06:04,530 --> 00:06:04,940
All right.

82
00:06:04,960 --> 00:06:08,100
And the one that I'm going to ask for is external storage.

83
00:06:08,400 --> 00:06:11,100
And the yeah, that's pretty much it.

84
00:06:11,130 --> 00:06:20,400
So we need to pass in manifest permission to read external storage.

85
00:06:21,540 --> 00:06:26,490
And then the last parameter here will be the code that we need to pass.

86
00:06:26,820 --> 00:06:27,240
All right.

87
00:06:27,240 --> 00:06:29,250
So read storage permission code.

88
00:06:29,580 --> 00:06:33,570
That's the code that we prepared, of course, in our companion object.

89
00:06:33,780 --> 00:06:37,980
OK, so this one here, as we can potentially ask for multiple permissions.

90
00:06:37,980 --> 00:06:39,570
That's why we have those codes.

91
00:06:39,570 --> 00:06:40,980
And we also need to pass in the code.

92
00:06:40,980 --> 00:06:45,090
So we start with the code one here for our storage permissions code.

93
00:06:45,330 --> 00:06:47,640
It could have been any number that we wanted.

94
00:06:48,390 --> 00:06:48,810
All right.

95
00:06:48,810 --> 00:06:50,600
So this is how we request the permission.

96
00:06:50,730 --> 00:06:53,760
Now we need to catch permission results.

97
00:06:53,910 --> 00:07:01,520
So it's over the on request permission results or permissions result.

98
00:07:01,860 --> 00:07:06,450
So we get the request code to get the permissions and we get the grant results.

99
00:07:06,670 --> 00:07:07,080
All right.

100
00:07:07,350 --> 00:07:16,350
So what we can now check is if the request code is equal to our read storage.

101
00:07:18,810 --> 00:07:20,400
Permission code.

102
00:07:21,320 --> 00:07:26,420
If that's the case, then we want to check if the grant results.

103
00:07:28,930 --> 00:07:29,770
He's not empty

104
00:07:32,290 --> 00:07:34,510
and the grant results.

105
00:07:37,690 --> 00:07:44,090
At the position zero, because our array could have multiple grant results here.

106
00:07:44,410 --> 00:07:52,900
It's an integer array, if that is the package manager, not permission granted.

107
00:07:54,130 --> 00:07:56,900
OK, so I think it's nothing new here, right?

108
00:07:56,900 --> 00:07:59,350
I'm just repeating what we have done multiple times already.

109
00:07:59,380 --> 00:08:05,020
So basically what we want to do is we want to get permissions and if we don't have permissions, we

110
00:08:05,020 --> 00:08:07,020
want to ask them for them.

111
00:08:07,030 --> 00:08:13,090
And if the user didn't give us the permissions, then we want to make sure that he understands that

112
00:08:13,090 --> 00:08:15,520
it's important that he gives us those permissions and so forth.

113
00:08:16,060 --> 00:08:17,500
OK, so if that's the case.

114
00:08:17,530 --> 00:08:25,150
So if the grant results aren't empty and we have the permission granted, what we want to do is basically

115
00:08:25,150 --> 00:08:25,960
the same thing.

116
00:08:26,350 --> 00:08:29,410
We want to show our image chooser.

117
00:08:29,500 --> 00:08:31,840
So show image user.

118
00:08:32,350 --> 00:08:37,299
And by the way, the same thing should be done in this if case.

119
00:08:37,520 --> 00:08:41,049
So if we already have the permission and we just ask for it.

120
00:08:43,559 --> 00:08:48,840
All right, and of course, what should happen if the user didn't give us the permissions that we wanted

121
00:08:49,110 --> 00:08:50,790
so he didn't grant us the permissions?

122
00:08:51,150 --> 00:08:51,930
Well, then.

123
00:08:53,810 --> 00:08:57,050
Let's just say you just denied permissions for storage.

124
00:08:58,340 --> 00:09:00,030
You can allow it from the settings.

125
00:09:00,050 --> 00:09:06,290
All right, so this is basically a little text to let you know that it's important that we have those

126
00:09:06,290 --> 00:09:14,390
rights or that he can still make changes, but in the settings of the app or for the app and Android

127
00:09:14,390 --> 00:09:14,900
itself.

128
00:09:16,810 --> 00:09:22,600
All right, I know at this point what I'm going to do is I'm going to create a new function, private

129
00:09:22,600 --> 00:09:30,690
fun show image chooser, and that one should just show or start an intent.

130
00:09:30,700 --> 00:09:30,960
Right.

131
00:09:31,330 --> 00:09:40,390
So let's create a gallery intent, which will be an intent which will have the intent action pick as

132
00:09:40,390 --> 00:09:40,870
action.

133
00:09:41,260 --> 00:09:51,880
So action pick and then it should go ahead and use the media store in order to access the images and

134
00:09:51,880 --> 00:09:55,600
then media dot external content urai.

135
00:09:57,490 --> 00:10:04,330
And this, of course, will just open up the functionality so that we can select an image from the storage

136
00:10:04,330 --> 00:10:08,470
of the user or the user can select an image from his own storage.

137
00:10:08,650 --> 00:10:09,040
All right.

138
00:10:09,400 --> 00:10:15,610
And of course, we want to start that activity with the request of a result because we want to get the

139
00:10:15,610 --> 00:10:17,490
result, which is going to be the data.

140
00:10:17,500 --> 00:10:23,110
So the image that we want then to store inside of this little icon here.

141
00:10:24,480 --> 00:10:25,950
So this icon, of course.

142
00:10:26,920 --> 00:10:37,480
So start activity for result gallery intent, and we are passing the code, which is our pig image request

143
00:10:37,480 --> 00:10:46,270
code, so that's the one that we prepared before in our companion object so that we can find out which

144
00:10:46,270 --> 00:10:50,190
request code it was, because we could have had multiple requests.

145
00:10:50,200 --> 00:10:50,520
Right.

146
00:10:50,890 --> 00:10:53,650
So multiple start activity for results.

147
00:10:55,510 --> 00:11:01,980
OK, now, at this point, we can, of course, call this function at the required to do points.

148
00:11:01,980 --> 00:11:08,200
So here, when the grant results are given, permission is granted and of course, if we already had

149
00:11:08,200 --> 00:11:09,490
the rights to use it.

150
00:11:09,820 --> 00:11:13,840
So here in our own, when we click on the image.

151
00:11:15,550 --> 00:11:21,250
Now, at this point, we need to look at something, the thing is, what we are using is we're using

152
00:11:21,250 --> 00:11:25,480
a functionality to display an image and we are using a third party library for it.

153
00:11:25,480 --> 00:11:25,750
Right.

154
00:11:25,780 --> 00:11:30,400
So that user data in UI is where we use Cleide.

155
00:11:30,820 --> 00:11:36,970
And if we look at light, what we can see is that it requires something which is a you or I.

156
00:11:37,000 --> 00:11:43,000
So this load here does load function requires a string, which is a urai.

157
00:11:43,360 --> 00:11:49,270
OK, so the location of the euro could be something in the web, but it also can be something on the

158
00:11:49,270 --> 00:11:49,810
device.

159
00:11:51,040 --> 00:11:57,850
We want to load something from the device, but we need to know where it is, so we ask for the location

160
00:11:57,850 --> 00:11:58,140
of it.

161
00:11:58,420 --> 00:12:05,020
So when the user selects an image from his phone, it's not like we're copying the image from the phone

162
00:12:05,020 --> 00:12:06,190
into the application.

163
00:12:06,190 --> 00:12:12,130
But we're just saying, OK, this image has a certain urai and that's the urai that we want to reuse.

164
00:12:12,130 --> 00:12:17,110
And that's the one that we will have to pass in here when we load the image.

165
00:12:18,540 --> 00:12:20,910
So now let's go ahead and get that.

166
00:12:22,260 --> 00:12:29,310
And therefore, I'm going to create a variable, actually, let me put that underneath the companion

167
00:12:29,310 --> 00:12:35,130
object, private var m selected image file.

168
00:12:35,700 --> 00:12:36,330
You're right.

169
00:12:37,980 --> 00:12:41,370
Which will be of type your I nullable

170
00:12:43,830 --> 00:12:46,350
and your eye can be.

171
00:12:47,240 --> 00:12:49,560
Transferred into a string easily, OK?

172
00:12:50,060 --> 00:12:52,610
But I still want to store it as your I.

173
00:12:54,180 --> 00:13:02,520
And I can go ahead and ask for the activity results, so we prepared this function where we have the

174
00:13:02,520 --> 00:13:09,450
image chooser and we start activity for result and of course, we want to do something when we get the

175
00:13:09,450 --> 00:13:10,340
activity results.

176
00:13:10,500 --> 00:13:14,040
So on activity result.

177
00:13:16,610 --> 00:13:22,630
And it's this one here, what we want to do is we want to check is the result code.

178
00:13:23,150 --> 00:13:26,810
OK, so did it work activity?

179
00:13:28,000 --> 00:13:29,170
Don't result.

180
00:13:29,200 --> 00:13:29,610
OK.

181
00:13:32,370 --> 00:13:34,650
And is the request code.

182
00:13:36,230 --> 00:13:42,580
The one that we prepared, this is the pic image request code, this one here, is it the same?

183
00:13:43,340 --> 00:13:46,110
And here's the data that we get something.

184
00:13:46,280 --> 00:13:47,540
So is it not now?

185
00:13:49,420 --> 00:13:53,690
All right, so that's basically what we need here.

186
00:13:54,160 --> 00:13:55,330
We need to get the data.

187
00:13:56,320 --> 00:14:02,650
We need to make sure that the reason the test was OK and so forth, OK, so once we know all of these

188
00:14:02,740 --> 00:14:13,600
things are true, what we can do is we can set our M selected image file urai to be DataDot data.

189
00:14:15,330 --> 00:14:23,400
So this data is what we get as a result here from our own activity result and from there we take the

190
00:14:23,400 --> 00:14:27,270
data itself and we store that in a selected image.

191
00:14:27,270 --> 00:14:28,050
File your eye.

192
00:14:28,980 --> 00:14:33,600
So if you looked at the data, you can see getData returns a urai.

193
00:14:34,740 --> 00:14:39,000
And that is beautiful because that's exactly the same type as our selected image.

194
00:14:39,000 --> 00:14:40,500
File your eye of type your.

195
00:14:41,710 --> 00:14:42,190
Perfect.

196
00:14:42,640 --> 00:14:48,160
So at this point, you can see that our you or I is going to be.

197
00:14:49,900 --> 00:14:57,220
Where the images and now what we can do is we can go ahead and use the same functionality where we load

198
00:14:57,220 --> 00:14:57,700
the image.

199
00:14:59,710 --> 00:15:03,010
So we basically just do the same thing from our Clyde.

200
00:15:04,390 --> 00:15:05,050
And here.

201
00:15:07,770 --> 00:15:14,610
And of course, the data will not be user image, but it will be the selected image.

202
00:15:15,060 --> 00:15:16,070
File your eye.

203
00:15:18,310 --> 00:15:23,890
Now, the thing is, this could potentially go wrong because we are loading something and so forth,

204
00:15:23,900 --> 00:15:30,460
so if you want to make sure that this is going to work, fine, we can put a try and catch around it.

205
00:15:30,810 --> 00:15:32,670
That's always a good idea in these cases.

206
00:15:32,680 --> 00:15:40,260
So let's have an exception here that will be captured or captured, caught so.

207
00:15:40,570 --> 00:15:41,260
So to speak.

208
00:15:41,980 --> 00:15:46,930
OK, and we're just going to print it onto this trace.

209
00:15:48,480 --> 00:15:53,640
All right, and at this point, we should be able to test this and it should work on our device.

210
00:15:55,530 --> 00:15:57,060
OK, so that we are.

211
00:15:58,340 --> 00:15:59,380
Pro-gay marriage.

212
00:16:01,820 --> 00:16:03,140
Now, let's go to our profile.

213
00:16:04,020 --> 00:16:09,730
My profile, you can see that the images in there, but it's not brought into the right format.

214
00:16:09,750 --> 00:16:11,730
Now selected, selected.

215
00:16:11,730 --> 00:16:15,210
So first of all, you can see it asked us for permission.

216
00:16:15,220 --> 00:16:16,050
We're going to allow it.

217
00:16:16,230 --> 00:16:19,530
And now let's select an image and I'm going to use Holmer again.

218
00:16:20,310 --> 00:16:22,630
But this time you could see it's slightly different.

219
00:16:22,650 --> 00:16:24,000
It's not exactly the same image.

220
00:16:24,000 --> 00:16:25,830
And it Krupp's it directly for us.

221
00:16:27,010 --> 00:16:29,540
All right, so that is it for this video.

222
00:16:29,920 --> 00:16:35,850
I hope you managed to get most of this done by yourself or maybe even you will get it done completely.

223
00:16:36,310 --> 00:16:39,100
That is definitely something that you should strive for.

224
00:16:39,370 --> 00:16:43,920
You should always aim for getting stuff done by yourself at this point or a lot of it at least.

225
00:16:44,260 --> 00:16:50,230
And of course, when something new comes in to play, then, of course, check out how I do it in order

226
00:16:50,230 --> 00:16:51,370
to fully understand it.

227
00:16:51,730 --> 00:16:52,060
All right.

228
00:16:52,060 --> 00:16:52,820
So thanks a lot.

229
00:16:52,840 --> 00:16:54,040
And see you in the next one.

