Welcome back.
And this video, we're going to implement the functionality so that we can actually click on this button
and then go to the gallery and select an image from there.
So therefore, inside of my request permission, I'm going to start an intent once I have granted the
permission, so once the permission was granted to me.
So if is granted the permission that we're looking at and that is our gallery permission, if that's
the case, then let's go ahead and pick an intent or create a new intent that will be called content.
And this will just be an intent.
So we have seen how to use contents before, right where we can move from one activity to another inside
of our application.
But you can also run intents in order to go to other applications.
And in this case, to the gallery.
So here we can use action.
I pick this one action pick and then here we go to the media store.
So we just say, I want to go to the media store, dot images, thoughts, media thoughts, external
content.
You are and let's import media store here and here.
It's external content you are.
OK, so this will be the intent that I need to create.
So this will be the intent.
And now we just need to find a way to start this intent.
So just starting this content will not be enough.
What we actually want to do is we want to be able to select the image and therefore we need to create
a launcher.
And how that works with the latest Android Studio version is by creating a activity result launcher
once again.
So the same what we have done with the request permission.
But this time we're just opening something else.
So here I'm just going to call this one open gallery launcher, which will be an activity result launcher
and this time of type intent.
And we also need to register for activity result here.
But this time I'm passing a contract that will not be a multiple request permission, but a start activity
for results.
So here start activity for results like so OK.
And this activity for result is a method that we need to execute here and then we can get the result
from that start activity for result request.
So here we just get the result and we can now work with it.
OK, so this lambda basically this part here register for activity result allows us to execute.
Why won't you run in the case of the result that we're given and it gives us the result at the same
time?
OK, so here what we can now do is we can check what kind of result that was.
So if the result, that result code was result?
OK.
So and also the result data isn't empty, so is not null, then I want to run some code.
So what is it that I want to run?
Well, I want to take that data that is given to me from that result.
And then I want to assign it to my image of you.
So what is the image that I want to assign it to?
Well, let's look at it.
Let's go over to our activity.
Main example.
And in here we have this image background.
What is it?
I think it's.
For our frame layout here.
So we have this Ivy background, that's our image view and that is currently this image here, and I
want to replace that with an image that we select from the gallery, right?
So in order to do that, what we need to do is, of course, to create a image background or an image
view here, find it by its ID.
And it will be brought ID Dot I've backgrounds.
And of course, I need to import image view here for this to work.
So now we have access to the background image.
So to this image view and we can now assign.
Data to it, so here.
Image, background, thought, set image.
And here you see we can draw both a resource, a bitmap and so forth.
But what we are getting is going to be a new array.
So let's use that.
You are I.
Because if you look at it, this media store image media data is external content.
You are a your eye is basically the location on your device, so it's like a path on your browser.
So here, for example, I have this path and that is where it is on my PC, right?
So users admin, desktop and road projects and you have the same thing with your images on your Android
device.
So there's also there's content your eye, which is basically this link that I just showed you.
So the location on the device.
And that's what we're getting and we're getting it from this intent.
Well, basically, we're getting it from the activity for result.
OK.
The result gives us this data and we can access it by using result data, which is invaluable.
You see, here we are.
We checked whether it was now.
So it is another tool that can be now thought data.
So this will then give us the actual data.
So the actual image.
So the result data's location status data.
OK.
So now we can use this open gallery launcher inside of our is granted here, so if we were granted permission
to access the gallery, we can try to access it by launching our.
Open gallery launcher with our intent.
So it is a lot of extra code that you need to set up, but that's how it's done with the latest version
of Android Studio.
I'm not a big fan of it.
I think the older version was a lot easier with starting activity for results and stuff.
But here's what you need to do is you need to have a activity result launcher.
You need to define what kind of launch it will be.
In my case, it will be based on an intent to register for activity results so that you can actually
wait for a result.
You need to use the activity result contracts with that activity for results and then you actually get
the result and you can check the result code.
If it was OK, so we got data back and if it's not empty, let's assign it to the image background.
So the data that we're getting, which is just the location of the data.
So we're not really getting the data, but we're actually getting the location of the data so that we
can just use the location.
So we're not copying the image into our application.
We're just setting the image based on where it is on your device.
OK, because otherwise you would duplicate data and you would create more data on your phone and it
would store it somewhere and so forth.
So the smart approach here is really just to say, OK, where is this image?
Let's just use it from there.
And yeah, let's just use that as a background.
So here we are, of course, creating the find if you by the reference, so that we can actually actually
access to some L image view that we created.
OK.
So at this point, let's check it out.
Let's see if the button will do something.
And we'll start the gallery in, so basically move to the gallery activity slash application.
So let's click on it and we see we were granted the permission.
So I don't have a photo here, so let's download a photo and let's just add no account.
So if you don't have photos on your device, you can just search for photos here.
I like to use Homer Simpson.
The good old Simpson.
And then you see, it's all beautifully written in German.
I accept.
And now let's use the doughnut eating Homer Simpson, so click on it or hold press.
Download the image and download it.
OK, now.
Let's go back to our application, select a photo inside of downloads, you can see we have Homer Simpson
and now we can start drawing Homer.
Let's use the fine color here, and you see we can now start drawing this beautiful guy with his beautiful
head and stuff like that.
OK, so generally you now have an idea, I guess, of how you can get access to.
Your gallery.
So let me draw this beautiful homer here, and you see amazing.
I should have become an artist, not.
There we are.
Beautiful, Homer.
He's super high on doughnuts.
OK, so that's it for this video now you know how to select images.
And of course, you can now add more images.
You can, I think, even go to the camera app.
Let's see if it works.
And it will go to well.
I don't want to allow to do that.
Let's make an image or take an image.
Let's go back.
Let's select an image you can see.
We can now select the image from our picture, and you see what we had in the background was safe.
So I still have parts of Homer Simpson here.
And in the background, we have the room.
OK, that's it for this video.
See you in the next one where we are going to add an undue button functionality because while I'm drawing
something right and then I want to undo it because it just doesn't fit.
And that's really something that we need to implement.
So see you there.