1
00:00:00,300 --> 00:00:06,990
The next thing I would like to add to the app is to store the drawing, so if I draw something here,

2
00:00:07,710 --> 00:00:14,880
then I want to be able to store it and therefore we need to allow the writing onto the device at once.

3
00:00:14,880 --> 00:00:20,280
We allow that permission was granted and then we click on it again and then we could see the file was

4
00:00:20,280 --> 00:00:23,520
saved on our phone.

5
00:00:24,210 --> 00:00:26,280
And yeah, let's get started.

6
00:00:28,050 --> 00:00:30,870
As usual, the UI is the easiest point.

7
00:00:30,960 --> 00:00:38,100
So the first one where we actually see something, so I'm just going to add another image button just

8
00:00:38,100 --> 00:00:43,200
underneath this ID brush, which means next to the idea brush and I'm going to call this one.

9
00:00:43,740 --> 00:00:45,030
I be safe.

10
00:00:45,930 --> 00:00:49,290
And then, of course, we also need to use another image here.

11
00:00:49,290 --> 00:00:52,440
So it's dragon and other trouble.

12
00:00:53,800 --> 00:00:56,740
And it's will be, uh, I see safe.

13
00:00:57,910 --> 00:01:04,450
So you're importing it with copy pasting, then we of course need to change to image here.

14
00:01:04,599 --> 00:01:05,590
So is he safe?

15
00:01:05,890 --> 00:01:11,290
Let's just see if this works and if the UI is as we want it to be.

16
00:01:13,300 --> 00:01:14,230
So there we are.

17
00:01:16,100 --> 00:01:18,350
The button exists, so the button is there.

18
00:01:18,530 --> 00:01:23,180
Of course, it doesn't do anything, so the first thing we need to do is to add the permission request

19
00:01:23,180 --> 00:01:28,460
because now we also want to write onto the device and not just read.

20
00:01:28,970 --> 00:01:29,360
All right.

21
00:01:29,540 --> 00:01:31,910
And then order two of right onto the device.

22
00:01:31,910 --> 00:01:36,080
We need to know where we want to write so at which path we want to write.

23
00:01:36,410 --> 00:01:42,530
Therefore, I want to create a new path, and I'm going to create a whole new directory in the resources

24
00:01:42,530 --> 00:01:43,010
folder.

25
00:01:43,400 --> 00:01:45,740
So new and directory.

26
00:01:45,740 --> 00:01:47,650
And I'm going to call this one example.

27
00:01:48,320 --> 00:01:53,840
So in here, I'm going to create a new example, and I'm just going to call this one path.

28
00:01:55,840 --> 00:01:57,910
Or maybe even Perth should be fine.

29
00:01:58,420 --> 00:01:59,260
Okay, so.

30
00:02:01,130 --> 00:02:05,990
I'm going to call this path, and it's not going to be a preference screen, but it's going to be some

31
00:02:05,990 --> 00:02:06,410
paths.

32
00:02:07,590 --> 00:02:13,890
So in here you can create your paths and dispatch file, which is our path out XML file.

33
00:02:14,190 --> 00:02:19,560
Maybe you could even call this one paths of XML because it could potentially have multiple paths and

34
00:02:19,560 --> 00:02:19,800
there.

35
00:02:20,040 --> 00:02:26,100
But we're just going to work with one and I'm going to work with an external path and it needs a name,

36
00:02:26,130 --> 00:02:32,280
and I'm going to call this one captured and then it needs to know where it needs to store it.

37
00:02:32,400 --> 00:02:38,880
So usually you would go like Android special data slash and then the package name of your project.

38
00:02:39,180 --> 00:02:45,890
So it's this one here at the very top, this one, in my case, you got tutorials dot kids drawing app.

39
00:02:46,650 --> 00:02:48,510
OK, so I add that in here.

40
00:02:49,750 --> 00:02:56,500
And then slash files, so I'm going to create this entry as an external path here.

41
00:02:57,730 --> 00:03:00,160
And then of course, we need to close that at one point.

42
00:03:00,820 --> 00:03:02,920
OK, so this is the path that we prepared.

43
00:03:04,420 --> 00:03:11,840
The next step is to add a provider to our manifest file, so in here, we already have this user's permission.

44
00:03:11,860 --> 00:03:12,460
Right?

45
00:03:12,910 --> 00:03:14,440
External storage, that's good.

46
00:03:14,740 --> 00:03:21,250
But now we also need to add a provider and we do that just underneath the activity within the application.

47
00:03:21,670 --> 00:03:24,040
So still in the application?

48
00:03:25,240 --> 00:03:26,950
We create this provider.

49
00:03:28,270 --> 00:03:38,050
This provider needs authorities, and here I'm going to use the name again of my package, that file

50
00:03:38,050 --> 00:03:38,770
provider.

51
00:03:40,220 --> 00:03:42,040
Then you need to have a name as well.

52
00:03:42,430 --> 00:03:47,950
And I'm going to use Android Hex Core.

53
00:03:48,370 --> 00:03:50,560
That content file provider.

54
00:03:51,280 --> 00:03:54,910
So that's going to be the name of the provider that I'm using.

55
00:03:55,120 --> 00:03:58,030
But we need a little more information inside of this provider.

56
00:03:58,030 --> 00:04:03,490
And actually, we're going to do it like this because we need to pass in something in there as well.

57
00:04:04,240 --> 00:04:05,980
But the provider is not done yet.

58
00:04:07,330 --> 00:04:13,480
And by the way, if you want to know more about the provider, it's something that you can look up and

59
00:04:13,480 --> 00:04:14,710
the documentation, of course.

60
00:04:15,040 --> 00:04:22,600
So inside the manifest file, you can create a provider that declares a content provider component and

61
00:04:22,600 --> 00:04:28,030
a content provider is a surplus of content provided the supplies structured access to data managed by

62
00:04:28,360 --> 00:04:29,170
the application.

63
00:04:29,170 --> 00:04:33,550
All content providers in your application must be defined in a provider element in the manifest file.

64
00:04:33,820 --> 00:04:36,370
Otherwise, the system is unaware of them and doesn't run them.

65
00:04:36,670 --> 00:04:40,870
OK, so we need to set that up if we want to store anything on the device.

66
00:04:41,530 --> 00:04:43,330
So that's exactly what we're doing here.

67
00:04:44,590 --> 00:04:46,030
And then the next thing?

68
00:04:47,740 --> 00:04:56,290
I'm going to set exported to faults, and what export it does is it indicates whether the content provider

69
00:04:56,290 --> 00:04:58,630
is available for other applications to use.

70
00:04:58,930 --> 00:05:05,950
So you can basically say, OK, whatever I'm providing could be used by another application.

71
00:05:06,220 --> 00:05:09,700
If I said that falls, the provider is not available to other applications.

72
00:05:10,300 --> 00:05:10,610
All right.

73
00:05:10,630 --> 00:05:15,170
If you set out to true to providers available to other applications any application can use to providers

74
00:05:15,190 --> 00:05:17,050
content, you try to access it.

75
00:05:17,260 --> 00:05:20,310
Subject to permission specified for the provider.

76
00:05:20,320 --> 00:05:25,270
So yeah, that's basically what you have to take into consideration.

77
00:05:25,270 --> 00:05:27,970
And then you also have to have this grant.

78
00:05:27,970 --> 00:05:29,080
You are permissions.

79
00:05:29,560 --> 00:05:35,290
So whether or not those who ordinarily would not have permission to access the content provider data

80
00:05:35,290 --> 00:05:37,180
can be granted to do so.

81
00:05:37,360 --> 00:05:43,420
Temporarily overcoming the restrictions imposed by the read permission by permission, permission and

82
00:05:43,420 --> 00:05:47,680
export it attributes through permissions can be granted and false if not.

83
00:05:48,280 --> 00:05:52,120
OK, so we're going to set that value to true in our case.

84
00:05:53,540 --> 00:06:00,020
Because granting permission is a way of giving an application component, one time access to data protected

85
00:06:00,140 --> 00:06:01,760
by a permission.

86
00:06:02,450 --> 00:06:04,400
OK, so let's set that up.

87
00:06:05,480 --> 00:06:09,510
So it is set to false and then we had to grant your permission.

88
00:06:09,560 --> 00:06:16,430
And I'm going to set down to drill now in here, I can create some metadata and the metadata that I

89
00:06:16,430 --> 00:06:20,000
need to give the provider is, of course, the resource.

90
00:06:20,750 --> 00:06:25,640
And another one is the name, and that metadata should have Android name.

91
00:06:26,330 --> 00:06:34,040
And here I'm going to use Android that support that file on the score provider.

92
00:06:35,900 --> 00:06:42,970
On score paths and the actual resource, will we our SML path?

93
00:06:44,170 --> 00:06:45,430
So an example.

94
00:06:47,330 --> 00:06:55,220
Path, so it's going to be this path that XML file, which is in our path called XML, so it's within

95
00:06:55,220 --> 00:06:57,260
the resources in the folder XML.

96
00:06:59,030 --> 00:07:01,130
And that's why I'm what I'm accessing here.

97
00:07:03,080 --> 00:07:10,580
So now that we have figured stuff out and the provider stuff which is required in order to provide anything

98
00:07:10,580 --> 00:07:14,570
or in order to you even store anything, the next thing we need to do.

99
00:07:16,940 --> 00:07:21,500
And by the way, of course, the authorities want to know more about the authorities.

100
00:07:21,980 --> 00:07:27,140
It's a list of one or more euro authorities to identify the data offered by the content provider.

101
00:07:27,650 --> 00:07:33,020
And typically, it's the name of the content provider subclass that implements the provider as it states

102
00:07:33,020 --> 00:07:33,270
here.

103
00:07:33,290 --> 00:07:34,250
There's no default.

104
00:07:34,250 --> 00:07:36,590
At least one authority must be specified.

105
00:07:37,160 --> 00:07:42,980
So in our case, we say that the file provider of our drawing app should be the one being used.

106
00:07:44,330 --> 00:07:53,540
OK, now let's go back to coding in our main activity because now what I would like to have is to get

107
00:07:53,540 --> 00:07:55,310
a bitmap from our view.

108
00:07:55,700 --> 00:08:01,550
Because if you look at our game or our app here, this whole area is a view.

109
00:08:01,910 --> 00:08:06,010
And now I would like to go ahead and make a bitmap out of it.

110
00:08:06,020 --> 00:08:10,610
Because bitmaps are images and bitmaps and images, they can be stored.

111
00:08:11,060 --> 00:08:16,820
Views themselves cannot be stored, so I need to convert it into a bitmap in order to do so.

112
00:08:16,970 --> 00:08:25,160
I'm going to create a new function here, and I'm going to do that a little further down here.

113
00:08:28,490 --> 00:08:39,289
And dysfunction that I'm going to create is going to be a private fun get bitmap from view and we need

114
00:08:39,289 --> 00:08:45,140
to pass on a view because I want this to return a bitmap.

115
00:08:45,770 --> 00:08:52,760
OK, so the idea is we get a view and we return a bitmap and we need to import bitmap here in order

116
00:08:52,760 --> 00:08:53,480
for this to work.

117
00:08:53,810 --> 00:09:01,160
And of course, we need to return a bitmap in the end, and the bitmap that I want to return is I'm

118
00:09:01,520 --> 00:09:04,010
going to be returned bitmap.

119
00:09:04,130 --> 00:09:06,920
So at least I'm going to call it like that returns.

120
00:09:06,920 --> 00:09:12,110
Bitmap is going to be bitmap that create bitmap.

121
00:09:14,900 --> 00:09:16,250
And he had this create bitmap.

122
00:09:16,910 --> 00:09:19,850
If we look at it, there are so many different versions here.

123
00:09:20,390 --> 00:09:26,900
And I want to use one where I pass and the width, so the width of the view.

124
00:09:27,800 --> 00:09:35,030
View that with 10 to height of the view viewed at height and then the bitmap type.

125
00:09:35,270 --> 00:09:38,420
So the bitmap configuration.

126
00:09:39,050 --> 00:09:48,410
So here bitmap that config dot aigbe and four eight.

127
00:09:49,780 --> 00:09:55,180
OK, so let's put that in another line, so it's a little more readable, so I'm creating this returned

128
00:09:55,180 --> 00:09:58,660
bitmap and I'm holding the return because I'm going to return it.

129
00:09:59,140 --> 00:09:59,470
All right.

130
00:09:59,470 --> 00:10:05,560
And it will take the whole width of the view, the whole height of the view, and it will take the configuration

131
00:10:05,560 --> 00:10:07,780
of IRG B eight, eight eight.

132
00:10:07,990 --> 00:10:12,220
And we saw that earlier, which pretty much just is a type of.

133
00:10:13,730 --> 00:10:21,350
Defining how many colors we want to have and also defining which type of opacity we want to have.

134
00:10:22,560 --> 00:10:22,970
OK.

135
00:10:23,390 --> 00:10:24,050
The next thing?

136
00:10:26,150 --> 00:10:32,330
Is that we want to bind to canvas that is on the view, so pretty much the thing that we drew.

137
00:10:33,410 --> 00:10:36,200
And in order to do so, I'm going to create another variable.

138
00:10:36,200 --> 00:10:37,580
I'm going to call this one canvas.

139
00:10:37,610 --> 00:10:42,680
It's going to be a canvas and it will take in the returned bitmap.

140
00:10:43,750 --> 00:10:46,450
So it's import canvas here as well.

141
00:10:47,200 --> 00:10:49,660
And then, of course, we also need to get the background.

142
00:10:49,870 --> 00:10:54,920
So overall, if we look at this image that we have here, we can have a background, right?

143
00:10:54,940 --> 00:10:58,750
For example, we can have our home being in the background.

144
00:11:00,980 --> 00:11:03,080
So let's use Homer once again.

145
00:11:04,710 --> 00:11:13,740
So then Homer will be the background of the view, the view itself is going to be the thing that contains

146
00:11:13,770 --> 00:11:14,700
our canvas.

147
00:11:15,000 --> 00:11:23,970
And on the canvas is where we have all of our colors and the bitmap is going to be the combination of

148
00:11:23,970 --> 00:11:24,510
those three.

149
00:11:25,020 --> 00:11:31,200
So putting them all together and making one image out of it, it's like putting them into a sandwich

150
00:11:31,920 --> 00:11:37,380
maker and you have all of those layers and now you're making one sandwich out of it and you can eat

151
00:11:37,380 --> 00:11:37,500
it.

152
00:11:38,470 --> 00:11:40,290
OK, so that's what we're going to do here.

153
00:11:40,740 --> 00:11:43,350
And therefore, we also need to know the background.

154
00:11:43,680 --> 00:11:49,200
So big trouble, which is viewed as background.

155
00:11:50,310 --> 00:11:55,440
So whatever we assigned as background, it can be that there is nothing, but it can be that it's the

156
00:11:55,440 --> 00:11:56,910
image that we just used.

157
00:11:57,330 --> 00:11:59,910
Homer Simpson for and if it's nothing.

158
00:12:00,120 --> 00:12:10,680
So if it's nothing we want to do or nothing, but if it is something, then we want it to draw on the

159
00:12:10,680 --> 00:12:11,130
canvas.

160
00:12:12,770 --> 00:12:18,950
Otherwise, I want to have a white background, so the canvas should be draw color.

161
00:12:21,170 --> 00:12:26,570
White, so coloured, white and here you need to to import color as well.

162
00:12:28,630 --> 00:12:29,260
Just like that.

163
00:12:30,010 --> 00:12:37,990
OK, so now we draw something, no matter if we have a background image or not, we now draw a background

164
00:12:38,170 --> 00:12:43,150
and then we need to draw the view the canvas onto the view.

165
00:12:43,210 --> 00:12:46,440
So draw canvas.

166
00:12:47,260 --> 00:12:50,770
And finally, we can return our return bitmap.

167
00:12:51,070 --> 00:12:54,580
So here return return bitmap.

168
00:12:57,810 --> 00:13:04,140
And by the way, by running this line, what we do is pretty much finalized the thing because what we

169
00:13:04,410 --> 00:13:09,370
prepared is our return bed map, right, which is taking the view.

170
00:13:09,370 --> 00:13:17,100
It's what we use with active use height and we then go ahead and we're prepared to canvas a little more.

171
00:13:17,430 --> 00:13:23,160
We put in the return bitmap into the canvas where we get the background.

172
00:13:23,760 --> 00:13:26,240
If there is a background, we draw that onto the canvas.

173
00:13:26,250 --> 00:13:32,710
If there's no background, we draw white onto the canvas and then we draw the canvas onto our view and

174
00:13:32,970 --> 00:13:34,860
find a good return or return bitmap.

175
00:13:35,980 --> 00:13:36,350
All right.

176
00:13:36,370 --> 00:13:42,850
And at this point, we are almost done, what we need to now prepare is a bit async task and this is

177
00:13:42,850 --> 00:13:48,310
a little bit of code, so I'd say, let's make a break here and finalize that code in the next video.

178
00:13:48,400 --> 00:13:49,120
So see you there.

