Welcome back and this video, we are going to add the functionality that allows us to store the image
on our device because currently it is not stored on the device.
It's, of course, in the gallery.
But the gallery could be changed by the user any time so he could go ahead and delete the image and
then, well, we cannot get the image anymore.
And we want to have the image, of course, in our happy place up here in the screen, where we will
then later on have all of our happy places and a list view.
All right.
So how do we get that?
How do we fix that issue and store the image permanently?
Well, we can just go ahead and take that image and then we'll just add the code to store it permanently.
And therefore, we are just going to go ahead and go back down here to our companion objects.
And I'm going to create a new.
Constant here, private contract value, and I'm going to call this one image directory.
Because that's going to be the folder on the phone where I'm going to store the image.
All right.
And the text that I want to.
Use here is happy places, images, and then I'm going to create a new method here, which I'm going
to call save image to internal storage, because that's what it should do.
And what should it store?
Well, it should store in bitmap.
And what shall this return, it shall return.
You were I saw the location of the image that we're storing.
All right.
So that's the idea of this method.
Then what I'm going to use is the wrapper and the wrapper should be our context repr.
And that is going to be the application context.
All right, so context, Pepper, I'm going to have has the application context to it.
So context, as you know, extends context, we saw that in the last video where we also used the.
Context repr before the content resolver.
OK, so now we can use the application context this way and then we need a file and this one can be
a variable because we're going to override it.
It should be repr dot, get directory.
And we need a context repr, by the way, in order to get the directory of our application because it
has a specific place on your phone in which you can store images, for example, or store files in general,
and we're going to use that.
So we're using this context that we created here to get the directory.
And I'm going to use our image directory here.
And then you can use context and different modes here, and I'm going to use mode, private mode, private
is the mode that allows me to make this file only accessible from the calling application or all the
applications that share the same user ID.
So basically other apps will not be able to access this directory, so they will not be able to access
our images to be stored in this application.
All right.
And then we can go ahead and.
Use file, person and file and the name of the file.
All right, so here we need to import file.
So here we got the directory.
Now we can use this directory to create a file.
So we say, OK, the file should be at this directory and then it should have a random unique identifier
name.
So here we can use this code.
Here you ID dot random.
UID.
Dot, jpeg.
OK, so it's going to store this image as a jpeg and it's going to give it a random, unique user ID
as the name, so to speak.
So this one will be random each time.
And by this we make sure that each image is unique.
And this is, by the way, the unique user I.D. you ID.
OK.
Now that we have the file, we can store it and you probably remember how we did that or maybe remember
it, we used the file output stream for this, OK, so we can go ahead and use file output streams and
stuff because things can go wrong.
We should use try here.
Right.
And then catch as well.
So I'm going to catch and input output exception here.
And I'm just going to say, if something goes wrong, then.
Right, stack, trace, so this is just going to be for us for testing purposes, if something goes
wrong, we will see that in our Lockette.
All right.
So then the next thing is that we need to create a stream, which will be our output stream, because
we're trying to output an image to our phone and it's going to be of type file output stream.
All right, in this final output stream needs to have a file that we need to pass in and the file is
the one that we created here.
So we're just saying, OK, use this file so the directory with this name and then go ahead and compress
our bitmap.
So here, bitmap thought compress with bitmap that compress format.
And the compressed format, I'm going to use JPEG here, the quality, I'm going to use 100 percent
here and then we need to pass in the stream.
All right, so the stream that we created here, so that is part of our compressed method, if you want
to know more about it here, compress method needs to compress format, the quality and the output stream.
All right.
And that's exactly what we prepared here.
So we prepared to output stream.
We have the quality setters.
One hundred and Tipitina that we want to use is going to be the pit map that was passed to this function
that would just create a.
OK, that's not it, we still need to add some more here.
For example, we should flush the stream once we're done compressing, so flush it and then close the
screen.
So stream that close.
Now, the method itself is, of course, still not happy because as you can see here, it's expecting
a URAI to be returned.
So now we, of course, need to return A or I thought pass filed absolute.
Absolutely.
So what did you hear is I'm saying, OK, our file has a path, right?
So we created a path here, the directory and the name.
So this is going to be the whole path, so to speak, so that the whole directory, the whole name,
how you can find the file and we use that.
And then we passed it into the format of a URAI.
And we returned it, so we're just creating pretty much whatever the format is that we can later on
use and that can be readable later on.
So the right format is a class again.
Of course, it's a.
An abstract class, as you can see here.
So if you want to know more about this, your AI class, you can check that out.
All right, so now what is left to do is we need to pretty much stop this or use this method that we
just created and a good spot would be in our own activity for result method.
So it's all the way up here on activity result, so here we are expecting a result and currently what
we're doing is we just set the image in our.
I've placed image, OK, so this is the selected image and actually.
We should do that here in our gallery, but also when taking an image.
OK, so here we're going to call this method, but it, of course, needs to have a bitmap.
And guess what?
We have this bitmap here, which is the selected image bitmap.
Now, the thing is, I cannot test it this way because we currently don't have any way of reading this
image from the storage.
So what I'm going to do is I'm just going to create a new variable and I'm going to call this one safe
image to internal storage.
So the same name as the method that we created.
And I'm going to create a little log here, log E, so that is nicely red.
So it's going to be an error log here.
Can, of course, use Ferbos or D or I hear as well, but he is definitely visible, so I'm going to
call this one safe image.
And then the message should be a path and our safety image to internal storage.
All right, so that's number one, and then we need to do it one more time.
So I'm just going to copy those lines here.
I'm going to do one more time when the images are thumbnail here.
So when we get it from the cameras, I'm going to put it in here and image, of course, will not be
our selected image bitmap, but it will be the thumbnail.
All right, and now we can go ahead and test to see if the image is actually stalled and where the stored.
All right, so let's select an image from the gamma gallery here.
And I'm just going to use this image here.
There we are, so now let's see in our Lockette if we get an error.
Somewhere, so here, Sarah.
And couldn't load my truck module, so there are a bunch of errors.
Maybe it's easier if we just search specifically for saved image.
All right.
So that we aren't saved image.
And the path is going to be data user zero, the name of our directory or of our project namespace.
Then app Happy Places Images.
And then that is to you, you had that we used all right, now we can test this once again.
But this time using the camera.
So let's add the image capture photo from camera.
It's just like any image here.
And we are so now we have a new unique image that was used, so these two images, they are stored now
and when we later on want to read them, we will be able to read them.
OK, so that is one of the first steps that we have here in terms of storing the images and then also
reusing them and displaying them.
But of course, there's still a lot to do in the next few videos.
OK, so I'd say see you in the next one.