The next thing I would like to add to the app is to store the drawing.
So if I draw something here, then I want to be able to store it and therefore we need to allow the
writing onto the device.
And once we allow that permission was granted, then we click on it again.
And then we could see the file was saved on our phone.
And yeah, let's get started.
As usual, the UI is the easiest point.
So the first one where we actually see something.
So I'm just going to add another image button just underneath this ID brush, which means next to the
idea brush and I'm going to call this one, I be safe.
And then, of course, we also need to use another image here.
So let's drag in another trouble.
And it will be the icy safe, so we're importing it with copy pasting, then we, of course, need to
change the image here.
So I see safe.
Let's just see if this works and if the UI is as we want it to be.
So there we are.
The button exists, so the button is there.
Of course, it doesn't do anything.
So the first thing we need to do is to add the permission request, because now we also want to right
onto the device and not just read.
All right.
And in order to override onto the device, we need to know where we want to.
Right.
So at which path we want to write.
Therefore, I want to create a new path and I'm going to create a whole new directory in the resources
folder.
So new and directory and I'm going to call this one XML.
So in here I'm going to create a new XML and I'm just going to call this one path.
Or maybe even yeah, path should be fine.
OK, so.
I'm going to call this path and it's not going to be a preference screen, but it's going to be some
paths.
So in here, you can create your paths in this path file, which is our path, that XML file, maybe
you could even call this one path of XML because it could potentially have multiple paths and there
but we're just going to work with one and I'm going to work with an external path and it needs a name
and I'm going to call this one captured and then it needs to know where it needs to store it.
So usually you would go like Android data slash and then the package name of your project.
So it's this one here at the very top.
This one in my case.
You got tutorials, DOT Kids Drawing app.
OK, so I add that in here.
And then slash files, so I'm going to create this entry has an external path here and then of course,
we need to close that at one point.
OK, so this is the path that we prepared.
The next step is to add a provider to our manifest file.
So in here, we already have this user's permission, right?
External storage, that's good.
But now we also need to add a provider and we do that just underneath the activity, within the application.
So still in the application.
We create this provider, this provider needs authorities, and here I'm going to use the name again
of my package dot file provider, and then you need to have a name as well.
And I'm going to use Android X hard core, that content file provider.
So that's going to be the name of the provider that I'm using.
But we need a little more information inside of this provider.
And actually we're going to like this because we need to pass in something in there as well.
But the provider is not done yet.
And by the way, if you want to know more about the provider, it's something that you can look up in
the documentation, of course.
So inside the manifest file, you can create a provider that declares a content provider component and
a content provider is a supplier of content provided the suppliers structured access to data managed
by the application.
All content providers in your application must be defined in a provider element in the manifest file.
Otherwise, the system is unaware of them and doesn't run them.
OK, so we need to set that up if we want to store anything on the device.
So that's exactly what we're doing here.
And then the next thing.
I'm going to export it to false and what exporter does is it indicates whether the content provider
is available for other applications to use.
So you can basically say, OK, whatever I'm providing could be used by another application.
If I said that false, the provider is not available to other applications.
All right.
If you said that the true the providers available to other applications, any application can use to
provide content, you try to access it subject to permission specified for the provider.
So that that's basically what you have to take into consideration.
And then you also have to have this grand jury permissions.
So whether or not those who ordinarily would not have permission to access the content provider data
can be granted to do so temporarily, overcoming the restrictions imposed by the permission, broad
permission, permission and export it attributes true if the machines can be granted and false if not.
OK, so we're going to set that value to true in our case.
Because granting permission is a way of giving an application component one time access to data protected
by a permission.
OK, so let's set that up so it is set to falls.
And then we had to grant Urai permissions and I'm going to set down to drill now in here I can create
some metadata and the metadata that I need to give the provider is of course the resource and another
one is the name.
So and that metadata should have Android name.
And here I'm going to use Android that support thought file on the score provider.
Underscore paths and the actual resource will be our example path.
So at Ximo.
That path, so it's going to be this path, that XML file, which is in our path called XML.
So it's within the resources in the folder XML.
And that's why I'm what I'm expressing here.
So now that we have figured the path of stuff out and to provide our stuff which is required in order
to provide anything or in order to you even store anything, the next thing we need to do.
And by the way, of course, the authorities so if you want to know more about authorities, it's a
list of one or more URAI authorities to identify the data offered by the content provider.
And typically, it's the name of the content provider suppliers that implements the provider.
Has IT states here there's no default.
At least one authority must be specified.
So in our case, we say that the file provider of our drawing app should be the one being used.
OK, now let's go back to coding in our main activity, because now what I would like to have is to
get a bitmap from our view, because if you look at our game or our app here, this whole area is a
view.
And now I would like to go ahead and make a bitmap out of it because it maps our images and maps and
images.
They can be stored.
Views themselves cannot be stored.
So I need to convert it into a bitmap in order to do so.
I'm going to create a new function here and I'm going to do that a little further down here.
And dysfunction that I'm going to create is going to be a private fun get bitmap from view, and we
need to pass on a view because I want this to return a bitmap.
OK, so the idea is we get a view and we return a bitmap and we need to import bitmap here in order
for this to work.
And of course, we need to return a bitmap in the end.
And the bitmap that I want to return is going to be returned bitmap.
So at least I'm going to call it like that.
Returned bitmap is going to be bitmap that create bitmap.
And he had this create bitmap, if we look at it, there are so many different versions here and I want
to use one where I pass and width.
So the width of the view butat with then the height of the view Furat height and then the bitmap type.
So the bitmap configuration.
So here bitmap dot config dot I rgv and four eight.
OK, so let's put that in another line so it's a little more readable, so I'm creating this return
bitmap and I'm calling the return because I'm going to return it.
All right.
And it will take the whole width of the view, the whole height of the view, and it will take the configuration
of Igby eight, eight, eight.
And we saw that earlier, which pretty much just is a type of.
Defining how many colors we want to have and also defining which type of opacity we want to have.
OK.
The next thing.
Is that we want to bind the canvas that is on The View, so pretty much the thing that we drew and in
order to do so, I'm going to create another variable.
I'm going to call this one canvas.
It's going to be a canvas and it will take in the returned bitmap.
So let's import canvas here as well.
And then, of course, we also need to get the background.
So overall, if we look at this image that we have here, we can have a background, right?
For example, we can have our Holmer being in the background.
So let's use Holmer once again.
So then Homer will be the background of the view, the view itself is going to be the thing that contains
our canvas and on the canvas is where we have all of our colors and the bitmap is going to be the combination
of those three.
So putting them all together and making one image out of it, it's like putting them into a sandwich
maker and you have all of those layers and now you're making one sandwich out of it and you can eat
it.
OK, so that's what we're going to do here.
And therefore, we also need to know the background.
So big trouble, which is viewed as background.
So whatever we assigned as background, it can be that there is nothing, but it can be that it's the
image that we just used Homer Simpson for.
And if it's nothing, so if it's nothing, we want to do nothing.
But if it is something, then we want it to draw on the canvas.
Otherwise, I want to have a white background, so the canvas should be draw color.
White, so colored that white and needed to import color as well.
Just like that.
OK, so now we draw something, no matter if we have any background damage or not, we now draw a background
and then we need to draw the view, the canvas onto the view.
So draw a canvas and finally we can return our return bitmap.
So here, return return bitmap.
And by the way, by running this line, what we do is pretty much finalized the thing because what we
prepared is our return bitmap, right.
Which is taking the view to what we use with Antabuse height.
And we then go ahead and we prepared to canvas a little more.
We put it in the return, bitmap into the canvas.
We get the background.
If there is a background, we draw that onto the canvas.
If there is no background, we draw white onto the canvas and then we draw the canvas onto our view
and finally we return or return bitmap.
All right, and at this point, we are almost done what we need to now prepare is a bit async task and
this is a little bit of code.
So I'd say let's make a break here and finalize that code in the next video.
So see you there.