Higher want.
So in this tutorial you're going to go ahead and we're going to add another functionality to this application.
So we are going to allow the user to save the image into actually external memory.
So here you can see that we have this button save the picture in order to do that.
So we need a class inside our project.
So let's actually at the top and just get the kind of view to Windows and then we can project.
So here it is our Android you and see it is the project you I need to change it to Android.
So here look at this little triangle here.
If I click on it I can select Android.
Now you can see that it is actually called the Android view.
If I open the app folder you can see that they have the manifest Java address.
So let's open the Java folder and let's open the first package that actually contains these two classes
the colorful class and the main theme with the class select expect package at the top menu just a click
on File New and then click on Dobber class.
So for the name of the class I can actually say save image or a safe flight save flight.
So this is the name of the class and the kind is class.
You don't have to specify the superclass or the interface.
This is the package visibility is public and the modifiers is none.
So just click on OK.
So here it is our class like puppy class.
Safe flight.
So now that the class is created let me double click on this tab here say find that job in order to
make it area there.
Now let's create a method in order to actually save the file to external storage.
So I can say Puplick static look at this method.
It is static.
What does this mean.
This means that other classes if they want to actually use this method method they don't have to create
an object of typesafe by class.
They just need to call this method.
On this say five class itself.
So that's why it is actually called static.
So you're already familiar with that right.
Public static.
And now I need to specify the return type of this method.
So the return type is going to be filed if it is going to return an object of type fight.
So what is going to be the name of that method.
The name of them if it is going to be safe flight.
Right that's the name of this method and this is going to accept some actually the arguments you need
to specify some parameters for this method.
The first priority is going to be off type activity.
So just they've been here activity and the name of this parameter is going to be my activity my activity
and then he'll put a comma.
The second parameter is going to be off the bitmap.
Right because you want to actually save a bitmap to external storage.
So the second parameter is going to be bitmap and the name is going to be bitmap.
So let's here specify that these Smurfette is going to throw an exception.
Right.
This method is error prone and it can actually go wrong and it's going to actually cause some errors.
So we are going to allow two other Nicolet other classes and know that the Smurfit is not actually completely
safe.
This method can actually for an exception.
So other classes if they want to use this method they must actually handle these exception exceptions.
Otherwise it's not going to work.
OK.
So I can say fros no exception.
Right.
So just open and close this method by using these curly braces and opening curly brace followed by a
closing curly brace.
Let's go inside the Smurfette And let's see what we are going to do next.
So create of idea but of types 3 in here so let's specify this data type yestreen.
So notice that this variable is actually going to be a local variable right because it is local to the
curly braces of this method.
So now we're going to specify the name of this value.
Well this is going to be external external storage state.
So if you want to actually know that whether the device has or has an external threat or not.
OK so if you want to be sure about that.
So I create a variable here.
I put an assignment operator and I'm going to refer to this environment class that actually knows about
whether the user has an external storage or not.
So environment that gets external storage.
State semi-hollow.
Now let's create a variable of type fight because we want to actually save this for it to extend or
to store it right.
Our image which is which is actually a file.
So let's create a file and notice that this method must return a value of a file.
So that's why I'm creating this value in here.
And the name of this variable is going to be my file assignment operator.
Initially I'm going to assign the value null to this variable because this is actually a local variable
and the value is not going to be assigned to this variable.
By default if it was an instance variable Yes it was the default value would be null.
But here it is actually a local variable.
And here we must actually specify an initial value for this value but otherwise later when we want to
use this variable it's going to actually show us some error.
The compiler is going to actually complain that this value is not initially initialized yet.
So initially we are going to assign the value and also you know what is the what not mean not means
nothing.
This valuable points to nothing.
So now I can't say if so I'm going to refer to this valuable external storage state but I'm going to
actually compared the value of this valuable to another value so I'm going to say equals right equals
and I'm going to is you can see that this equals my if it needs an argument of type Object.
Right.
So I'm going to say equals environment that media mountains.
What does this mean.
This means device has an external storage and it is ready and we can use it.
Right.
Environment that media mounted we can use the external storage if the external of the storage media
it does have this value then in that case we are a hundred and fifty percent sure that the device has
an external storage and we can save our file to that storage.
So let's go inside the curly braces of this if statement if statement and let's see what you're going
to do next.
But first you can see that what happens if this condition evaluates to false if what happens if the
user actually use this device doesn't have an external storage.
In that case you are going to have an else if an else statement.
So here I can say else and then I can open and close this whole statement by using these curly braces.
And here we can handle this condition.
So here I'm going to actually follow on exception.
I can say fro new exception new exception and inside the prentices I need to specify the message so
I can just put two codes here and I can say this device.
This device does not have does not have to be an external that an external storage.
Right semicolon.
So let's go inside this if statement here.
Let's create another variable of type file here.
So just typing here file the name of this variable is going to be pictures directory pictures directory
right.
Assignment operator.
Now I'm going to refer to this my activity argument that I receive from the Smurfette say fight right.
I can see my activity that my activity actually my activity that gets external storage file directory
get external.
Actually the file still use this method here.
So here we are way to actually pass a value of type string here and that value is going to specify a
directory of the pictures that we want to save into our external storage.
So here the name is going to be the name of this folder so that you're going to see that for that.
But for now just know that you're going to actually create a directory here or a folder in order to
hold all of the color pictures so I can put two quotes here and here instead the quotes I need to specify
the name of that folder.
So it is going to be color up pictures like color.
So here let me tell you that every time they use There's actually a princess on the Save the picture
button let me show you the activity underlayment examined here every time the user's presses on the
spot and save the picture here all you want a new picture to be created and I want that image to be
saved to external storage.
Right.
So every time the user presses on this button a new image is going to be created on your picture is
going to be created with these values.
For example red green blue and that image is going to be saved to our excellent storage.
But here we have a problem.
We must give a unique name for each image.
OK.
Two images with the same name cannot be existed in the same folder.
Right.
So we must give each image that the user actually creates by clicking on the Save the picture button.
We must give each image a unique name.
So let's see how we can do that.
Let's go to the save file class below this line of code.
I'm going to create a variable of type date right date which is in the Java that you can package date
and here the name of this variable is going to be current dates current date assignment operator new
date semicolon.
So when I actually create a new date object it is going to give me the current date.
OK the current date and this object is going to be assigned presumably but right.
Let's create another variable here.
And this is a blank right.
It is like is actually a little bit like integer but it's going to hold more values more larger values.
So learn the name of this variable is going to be elapsed time elapsed time.
Assignment operator and here I'm going to refer to this class system clock that actually elapsed real
time semicolon.
So let me show you the web browser here because I actually opened on you of a page here in stack overflow.
In order to let you know about this elapsed real time effort.
So take a look at this answer.
Here it says here is a little bit about a concept.
It says elapsed your time effort and elapsed through time none was perfect.
So here we are using the elapsed time if it's right.
Return that time since the system was booted.
This is really important.
Let me read it again.
The elapsed real time effort is going to return the time since the system was booted so let me go back
to Andrew studio.
What does this mean.
This means that every time the user wants to actually save the image so as a result the user has to
actually create a new image right because you want to create a new flight every time to use.
It creates a new picture by clicking on the Save the picture button that image is going to have a unique
elapsed time.
OK.
A unique elapsed time.
Let me show you.
That again is going to return the time since the system was booted.
So this elapsed time here is going to be unique.
Every time the user clicks on that button because it's impossible to use that actually create two pictures
with the same elapsed time.
Right.
Because the time has passed by that moment the user presses that button.
So if this doesn't make sense don't worry you can actually put the discussion in the course or you can
message me in the ME and I will do my best to answer your question.
But for now just make sure to actually look at the end of the documentation or watch this tutorial again
in order to understand what does this elapsed time mean.
OK.
Searching the internet just search for this elapsed realtime effect and then you get to get what I mean.
So and we have this elapsed date elapsed time.
Now I can't say.
So let's create a variable of types 3 and in order to specify that unique filename that's unique imagining
like strange and the name of this variable is going to be unique emerge.
The assignment operator so here I can say I can't put two coats.
I can I can put us last year plus I'm concatenating this value of another value plus current state plus
unable to Cote's yet underline plus elapsed time plus two coats or image or image format is going to
be actually of type P and G.
Right.
So I'm going to put that P and G that says semicolon.
This is going to be the unique image name that the user can actually create.
A bunch of times like unlimited times the user can create image a unique image names with unique names
right in the same folder.
That's the trick.
This is really important.
This is the trick.
In order to create unique filenames in the same folder.
Use it in your own projects.
So after this line of code I can say.