Welcome back.
In this video, we are going to implement the functionality to add an image here, so to select an image
from our storage and then use it in our profile.
So, for example, I can click on this icon, then it will ask me for permission.
I'm just going to allow it for this case and then I can go ahead and select a photo from the camera.
So let's select our Homer once again and then it will populate the image accordingly, as you can see
here.
All right, so you should already know how to do that, right?
You have seen that multiple times.
The thing is, what we will start with is the Internet connection, because we also want to have Internet
rights, so to speak.
Of course, also storage rights, as we will use that as well.
So let's go over to our Android manifest and use those permissions or add those permissions here, because,
as I said, we will need them.
And then the rest you can try to do pretty much yourself.
Right.
So you know how to do all of the steps that are necessary for this.
So you can just post the video and try to implement that.
That will definitely improve your development skills.
So go ahead and try it.
All right, so I hope you tried it and were successful, so what I'm going to use here is the Internet,
so this is permission and No.
One.
Next, we need to permission for the network state.
So network state, this one here and then the one for external storage.
So external storage.
And for now we're just reading external storage.
We're not going to store anything.
As of now.
If you want to store anything on the external storage, you can of course, also add the right external
storage permission.
But this is going to do it for now.
So now let's go over to our profile activity.
Because here is where the magic will happen, but I just want a little thing is I want to make sure
that my image view has an I.D. So I called this circle image view.
I see more I.V. user image.
Maybe we should make it more precise because this is the profile user image.
OK, so it's going to be unique now because I think we used IV user image somewhere else already.
So it always makes sense to have IDs as unique values.
All right, and as it is with permissions, you maybe recall it makes sense to have constants for the
permission codes.
So let's go ahead and create a companion object which will have the private constant value read storage
permission code, which will be one.
And then because we know we will also need the image request code.
Let's just add that as well.
So pick an image request code and let's assign to value to to it.
All right, so when should anything happen or something happen?
Well, as soon as we click on the button and by the way, here said user data.
So now it's, of course, renamed.
So I've provided user image, just a new name.
And they said user data UI, it's OK in our uncreate.
We can now go ahead and give our image view and on Click Listener.
So I've profile user image set on Click Listener so that we can execute some code in here.
And of course we want to do is we want to check if we have permission already.
So if context compact.
So here context compared check self permission.
And here we ask for the external reading, so here we need to pass in the context and by the way, let's
import context compared.
Just so the area disappears, so we need to pass in the context and then the permission that we want
and we will just get it from manifest permission to read external.
Your external storage.
All right.
And here we also need to import manifest would have been easier if we had imported it before.
Because then it will tell us which one of those is correct or incorrect.
OK, so that is the request that we have.
And then, of course, we want to execute some code in the case that we have this permission.
And we, of course, need to check it, because this is just a check here.
Is it permission granted?
So here, package manager.
That permission granted.
So this is always the structure and I think it makes sense to have it in multiple lines.
So we're saying if this here is equal to package manager, permission granted, OK?
And if that's the case, then what we want to do is we want to show the image chooser.
So we want to select the image pretty much.
Otherwise, so in the case, if we don't have permission and that's what we can implement before we
implement that one, so otherwise we want to ask for permission.
So activity compact's requests, permissions.
And here, permissions, let's import activity combat here as well.
OK, and then in here, we need to first in person the activity and then the array of our permissions
that we want to ask for.
All right.
And the one that I'm going to ask for is external storage.
And the yeah, that's pretty much it.
So we need to pass in manifest permission to read external storage.
And then the last parameter here will be the code that we need to pass.
All right.
So read storage permission code.
That's the code that we prepared, of course, in our companion object.
OK, so this one here, as we can potentially ask for multiple permissions.
That's why we have those codes.
And we also need to pass in the code.
So we start with the code one here for our storage permissions code.
It could have been any number that we wanted.
All right.
So this is how we request the permission.
Now we need to catch permission results.
So it's over the on request permission results or permissions result.
So we get the request code to get the permissions and we get the grant results.
All right.
So what we can now check is if the request code is equal to our read storage.
Permission code.
If that's the case, then we want to check if the grant results.
He's not empty
and the grant results.
At the position zero, because our array could have multiple grant results here.
It's an integer array, if that is the package manager, not permission granted.
OK, so I think it's nothing new here, right?
I'm just repeating what we have done multiple times already.
So basically what we want to do is we want to get permissions and if we don't have permissions, we
want to ask them for them.
And if the user didn't give us the permissions, then we want to make sure that he understands that
it's important that he gives us those permissions and so forth.
OK, so if that's the case.
So if the grant results aren't empty and we have the permission granted, what we want to do is basically
the same thing.
We want to show our image chooser.
So show image user.
And by the way, the same thing should be done in this if case.
So if we already have the permission and we just ask for it.
All right, and of course, what should happen if the user didn't give us the permissions that we wanted
so he didn't grant us the permissions?
Well, then.
Let's just say you just denied permissions for storage.
You can allow it from the settings.
All right, so this is basically a little text to let you know that it's important that we have those
rights or that he can still make changes, but in the settings of the app or for the app and Android
itself.
All right, I know at this point what I'm going to do is I'm going to create a new function, private
fun show image chooser, and that one should just show or start an intent.
Right.
So let's create a gallery intent, which will be an intent which will have the intent action pick as
action.
So action pick and then it should go ahead and use the media store in order to access the images and
then media dot external content urai.
And this, of course, will just open up the functionality so that we can select an image from the storage
of the user or the user can select an image from his own storage.
All right.
And of course, we want to start that activity with the request of a result because we want to get the
result, which is going to be the data.
So the image that we want then to store inside of this little icon here.
So this icon, of course.
So start activity for result gallery intent, and we are passing the code, which is our pig image request
code, so that's the one that we prepared before in our companion object so that we can find out which
request code it was, because we could have had multiple requests.
Right.
So multiple start activity for results.
OK, now, at this point, we can, of course, call this function at the required to do points.
So here, when the grant results are given, permission is granted and of course, if we already had
the rights to use it.
So here in our own, when we click on the image.
Now, at this point, we need to look at something, the thing is, what we are using is we're using
a functionality to display an image and we are using a third party library for it.
Right.
So that user data in UI is where we use Cleide.
And if we look at light, what we can see is that it requires something which is a you or I.
So this load here does load function requires a string, which is a urai.
OK, so the location of the euro could be something in the web, but it also can be something on the
device.
We want to load something from the device, but we need to know where it is, so we ask for the location
of it.
So when the user selects an image from his phone, it's not like we're copying the image from the phone
into the application.
But we're just saying, OK, this image has a certain urai and that's the urai that we want to reuse.
And that's the one that we will have to pass in here when we load the image.
So now let's go ahead and get that.
And therefore, I'm going to create a variable, actually, let me put that underneath the companion
object, private var m selected image file.
You're right.
Which will be of type your I nullable
and your eye can be.
Transferred into a string easily, OK?
But I still want to store it as your I.
And I can go ahead and ask for the activity results, so we prepared this function where we have the
image chooser and we start activity for result and of course, we want to do something when we get the
activity results.
So on activity result.
And it's this one here, what we want to do is we want to check is the result code.
OK, so did it work activity?
Don't result.
OK.
And is the request code.
The one that we prepared, this is the pic image request code, this one here, is it the same?
And here's the data that we get something.
So is it not now?
All right, so that's basically what we need here.
We need to get the data.
We need to make sure that the reason the test was OK and so forth, OK, so once we know all of these
things are true, what we can do is we can set our M selected image file urai to be DataDot data.
So this data is what we get as a result here from our own activity result and from there we take the
data itself and we store that in a selected image.
File your eye.
So if you looked at the data, you can see getData returns a urai.
And that is beautiful because that's exactly the same type as our selected image.
File your eye of type your.
Perfect.
So at this point, you can see that our you or I is going to be.
Where the images and now what we can do is we can go ahead and use the same functionality where we load
the image.
So we basically just do the same thing from our Clyde.
And here.
And of course, the data will not be user image, but it will be the selected image.
File your eye.
Now, the thing is, this could potentially go wrong because we are loading something and so forth,
so if you want to make sure that this is going to work, fine, we can put a try and catch around it.
That's always a good idea in these cases.
So let's have an exception here that will be captured or captured, caught so.
So to speak.
OK, and we're just going to print it onto this trace.
All right, and at this point, we should be able to test this and it should work on our device.
OK, so that we are.
Pro-gay marriage.
Now, let's go to our profile.
My profile, you can see that the images in there, but it's not brought into the right format.
Now selected, selected.
So first of all, you can see it asked us for permission.
We're going to allow it.
And now let's select an image and I'm going to use Holmer again.
But this time you could see it's slightly different.
It's not exactly the same image.
And it Krupp's it directly for us.
All right, so that is it for this video.
I hope you managed to get most of this done by yourself or maybe even you will get it done completely.
That is definitely something that you should strive for.
You should always aim for getting stuff done by yourself at this point or a lot of it at least.
And of course, when something new comes in to play, then, of course, check out how I do it in order
to fully understand it.
All right.
So thanks a lot.
And see you in the next one.