So here it's time to initialize or file Vaadia that we created here inside our Meffert save say it in
line number 24.
So here we are inside the safe and Meffert inside this savefile class actually.
So after this line of code here in line number 32 that actually specifies the unique image name I'm
going to initialize my valuable my file actually the modified assignment operator on here I can see
a new file.
Right.
So we need to pass the pathname.
Look at this argument here.
It says to pathname So the pathname is going to be the directory.
We created the directory here right.
Peteris directory.
Right.
Avi folder color pictures plus the filename the unique image and so I can say pictures directory plus
unique image name.
So this is going to be our file.
So we need to write this file to an external storage.
So now it's time to specify that whether or external storage has free space or not.
So let's actually create a variable of type long and the name of this valuable is going to be remaining
space remaining space assignment operator and I'm going to refer to are actually to our directory which
is going to be pictures directory right up this piece history be valuable.
So I can say pictures directory dot just type in here.
Free get free space.
Semicolon.
So it's going to return to free space that is remained inside or extend out of storage and it's going
to assign it to this valuable remaining space which is after lunch.
Right.
And notice that the return value is going to be off type bytes.
OK.
It's going to return the free space in bytes.
Now it's time to specify that how many actually it's space in bytes are actually needed in order to
save our bitmapped to external storage.
Right.
So I'm going to again create another variable of type at lunch and I can see the required required space
assignment operator and this method has an argument of type bitmap right.
So when I refer to this bitmap here bitmap that gets byte count semicolon.
So it's going our return to count of the bytes of this bitmap object of this image actually.
And it's going to assign it to this value.
But now it's time to create an if statement.
So if I'm here I'm going to actually refer to the required space if remaining if required space multiplied
by one point eight is less than remaining space.
Let's open and close this statement by using this curly braces.
So what does this mean.
Let me explain to you what's going on here.
Exactly what does this mean here.
This means that if the required space which is going to be the count of the parts of this bitmap object
right the image multiplied by this value of one point eight.
If the result of this operation is less than the value of the remaining space.
This means that we have enough space inside our external storage in order to save our image there.
Right.
So this is a formula you can use it later here.
This operation here is very important in order to specify that whether we have a remaining space here
in order to save our image to a central storage.
So the quiet space multiplied by 1.8 is less than remaining space.
Otherwise what happens if this condition evaluates to false.
What happens if the device is external storage doesn't have enough space in order to save the image.
In that case we need to create an L statement so let's create an alpha statement here and open and close
this whole statement by using this curly braces inside the curly braces of this Else statement I say
fro new exception for an exception.
And here we need to specify the message right.
Just put two coats and here I can say there is no there is no enough space in order to save the image
in order to save the image
to external storage right semicolon let me create a new line here so that you can see these values better.
So I actually of a front a new exception if this happens.
So now let's go inside the statement here.
So we are going to assume that the use of the user's device external storage has enough memory has enough
storage in order to save that bitmap.
I'm going to say because the operation of saving the image to external storage is going to be error
prone.
OK.
In that case we must actually put our code inside a try catch block so let's create a try catch block.
So try and after the ending curly brace of the stripe like I need to say catch inside Apprentice's just
they've been here.
Acception and the name of this prime it is going to be just open and closed this test played by using
these curly braces inside this tetch black church.
I can actually for a new exception.
So here I can see fro new ARE YOU exception for are exception.
I just put two coats here and Hitler can say that the operation went wrong.
I can say that the operation the operation of saving the image to external storage when it's rank semicolon.
So let me create a new line here.
External.
Actually let me create a line in order so that you can actually see these values better.
So now let's go inside this try block here.
In order to save our image in order to write our image to external storage we need to have an object
of type file output stream.
So let's create that here.
Let's create a value of type file of output stream inside a double that are your package and the name
of this rally but it's going to be five out with stream assignment operator new file output stream.
And here as you can see if you need to pass the file name right.
So I'm going to pass my fight here to this constructed of this final thoughts because I want to write
my file to extend our storage so that's why I'm passing them my file here to this constructor.
Now I can say boolean.
Let's create a variable of a boolean.
And here I can say is image saved successfully
assignment operator now I'm going to refer to my peace map arguments that I receive from this safe while
I'm here right.
So I'm going to say bitmap does compress bitmapped that compress.
And we need to pass some arguments to this.
The first one is going to be actually the compressed format so I can say bitmapped that bitmap with
chapter b right that compressed format that I told you that the format of our images that are going
to be saved to external storage is going to be of the P.A. which was PND here bitmapped that compressed
format that PNB the second argument is going to be the quality of the image.
So the best quality is 100.
But because we are here we are using the P.A. format.
It doesnt really matter which value you use.
OK.
The P.A. format doesn't really care about the quality that we specify here.
So we just passed a value 100 here because 100 is the best quality comma and the first argument here
is going to be file output stream.
So this object is going to actually write our image to external storage with these actually values.
For example the Pinti and the quality 100 and this object is going to just right this image to extend
our storage and the result of the Smurfette.
If this operation is successful and if the image is returned to extend out of storage successfully The
result is going to be true and its going to be assigned to this value.
But is image safe to successfully.
So let me create an if statement here.
Let me first let me create a new line here so that you can see the entire code here.
Now I'm going to actually create an if statement so if is image saved successfully I can often enclose
this statement by using these curly braces.
I can say return my flight.
So this means that I can return to my file object from this method.
Otherwise let's create an else statement else.
I can create an else statement here right.
I can throw an exception.
I can see fro new exception for nucula exception and instead the apprentice I can specify the message.
So here I can say the image the image is not saved is not safe.
Successfully
external storage external storage right semicolon.
So let me create a new line here so that it is RMF it and it can see that we have no errors here.
This is our fate that we're going to use later.
So our main activity class in order to save the image that is taken by the user by using the camera
to extend our storage.
So let's see how we can use this class and this method.
First we need to go to the Antrix manifest that exemplified here and we need to add another permission
to this file.
So below it is a permission that we added here I can actually open another tab and I can see the user's
permission and I'm going to say you write external storage and it's self close this time.
By using this by putting this dash and this closing tag here.
So we are going to ask the user to allow us to access the external storage.
And so when I say right this means that I can write and read.
But we have the permission that that is actually read.
Then Allosaurus really excellent.
So this means that we can only read the external storage but right then all the storage permission means
that we can read and write.
So let's go to our main activity class and let's find the unclick Smurfette.
So let me search for the anklets of.
If you are using a match you just need to hold down the command channel and then press and then that
if you keep both.
And if you are using a Windows machine you just need to hold down the control on your keyboard and then
press underlay that if key on your keyboard in order to search for a specific key word inside your file.
So I'm going to search for and click.
So he's going to find all the keywords with this value here.
Unclear here that it's actually match this value.
So the first one is viewed that Uncle listener.
So in order to find the next result here you can see these arrows here.
Right.
So let's click on this than our next occurrence and here we have this one this one.
And finally this one this is our uncle Meffert right.
If you want to you can just write the entire and Mefford of the parameters also.
So we have this uncle Mefford.
And here we are saying that if they view that good ID is equal to or ID that here and take a picture
then it's going to execute these codes here.
Right.
So what happens if the user clicks on these safe pictures so we created this LCS.
They've been elusive view that good idea is equal to our idea that B.T. and see feature.
So now let's go inside this LCF statement inside the curly braces of this elusive state and actually
and here I can't say double meaning to ask the users to give us our runtime permission in order to access
the external storage and to write the X then storage.
So let's create about the level of that integer here and there the name of this rally is going to be
permission check permission check.
Right.
Assignment operator and here I can say context compact that check self permission.
So we need to first we need to pass the context right.
So the contest is going to be main activity that this Kamo and the second value is going to be the actual
permission right.
So I can't say manifest that permission.
That's right.
External storage.
Semicolon.
Right.
So this is our permission here.
Now I can create an if statement so if permission check is equal to package manager that permission
granted this means that the user has allowed this application to access the extent of the storage then
only in that case we can actually write our code in order to save the image to external storage.
But what happens if the user doesn't allow us to access the extent of the storage or the user hasn't
allowed us to access the this and also this yet.
In that case we want to ask the user to give us the access to permission to access the sin of storage.
So as let's open this else they by using the curly braces on opening curly brace followed by a closing
curly brace.
And here I can ask to use there so I can just say activity Compaq's that request permission that request
permissions.
So first we need to pass the context so I can see main activity that is right.
Comma.
And I told you that we need to pass an army of permissions.
Here you can see that here right an army of permission.
So I can see new screen arii.
So I just put the two curly braces here and instead the curly braces I can pass the permission so we
only have one permission here.
It is going to be manifest.
Got permission that external storage.
Right.
Excellent.
So that's right.
Comma.
Finally we need to pass the request code.
The request must be a unique value so we can actually say 2000.
Right.
That's it.
Now that we specified that request.
Here let me create a new line here so that you can see this value better.
Let's go inside this statement here that says that permission is granted.
Right.
I need to because I want to call this save the file Meffert of the Save for class and that if it is
going to throw an exception we need to put our coats inside a try catch block right.
Otherwise the compiler is going to complain.
So just typing here try and after the adding curly brace of this try block.
Just step in here.
Cheche exception we just open and close to a catch block by using these curly braces and just like in
here.
Either print stack trace inside a try block here.
So this print stack trace.
If it is going to output some values to the LUGG if anything goes wrong inside this tribal I can say
safe file.
Class and because that Meffert they say fine if it inside the safety class is actually a static method
I can directly call that Meffert on this class.
I can say say that safelight need to pass some arguments to the Smurfette the first one is the activity
right.
So I can just say this right.
All I can say main activity that is main activity that's this karma.
The second argument is going to be bitmapped so I have an instance variable of that bitmap that holds
the image that it's early days that is going to be taken by the use it by using the Chamitoff the device
bitmap.
So if this line here executes successfully This means that the image is saved to external storage successfully
so if you are going to show a message a toast to the user that the image is now successfully say to
extend our storage so I can say toast that make takes the first minute to pass the context main activity
that this come on.
The second argument is going to be the message to code so I can say the image is now successfully success
fully saved.
To extend our storage and here finally we need to specify the duration.
So Len short thought show.
Let me create a new line here.
So you can see these post-mission message here.
So that's it.
Now it's time to run the application.
And let's see what happens.
So just choose one of these devices and then click on OK.
So now you can see that our application is running on the simulator.
Let's click on take a picture here.
Let's take a picture let's accepted let's now let's change these settings here.
For example this color or blue color.
And let's click on Save the picture.
Now it says allow app to access photos media and files on your device.
Yes.
We need to click on this button again save that picture.
Now it says the image is now successfully saved to external storage.
But it you don't have to find the image in extend all the storage here.
It's important to know that the image is saved to external storage.
But if you want to find that image it's better to test this up on your actual device.
So in fact I'm going to test it on my dogs and I'm going to show you how we can find the image on your
actual device.
So let me just stop the program.
Let me run the app again.
Now this thing I'm going to actually choose my device.
Let's click on OK.
So he's going to now run the app on my actual device.
But now the app is running on my actual device and I'm going to take a picture first.
I'm going to allow the permission and take your picture and then I'm going to just save the picture.
So it says we are going to access the directories.
Yes save the picture.
Now it says the image is saved to X and I'll sort of success with it.
So in order to find this image if you're using your actual device you need to have a file explorer or
for example some software that allow you to explore your files in Android such as file explorer or any
other apps.
I just need to open my file file manager right.
And then I need to go to the actually the Android we have a folder here called Android open it and we
have a folder called data.
Just open it and then you can actually find the package of your application.
So the package of our application is this one right for me is that example that monitors I saw that
the color app.
So I need to find this package here inside this data folder.
So let me find it here.
So I found it and insert is a folder we have a folder called files.
If I open it I can see some actually some folders here.
So I can open them and then I can see my images here.
So again it's not necessary to find your images in your external storage or in the previous tutorials
of this course I talked about accessing our images and how to save all the magistrates out of storage
and how to get images from an accident on the search.
But in this application it is not very important.
We just need to be sure that this image is safe to extend.
So that's it.
So we added this functionality to this application.
Right.
But it's not complete enough.
We want to share this image to other users.
Let's see how we can do that.
We've got to share this image actually with other apps such as e-mail or any other actually app applications
that can interact with other people such as messaging app such as whatsapp telegram or any other applications
or e-mail G-mail.
And then the next tutorial we're going to add that functionality to us to go out to our application.
Thank you very much for watching.
If you have any questions about the concepts that I taught you in this lesson please post the discussion
in the course and then I'll do my best to answer all of your questions.