Now it's time to finally get to the point where we can select an image as the background image for our
app.
And I'd say we start by picking a photo and in order to pick a photo, what we need to do is we need
to add a bunch of code that will run once we have established that we have permission to read the story.
So let's go ahead and do that.
What I'm going to start with is with a variable, which I'm going to call pick photo intent, and it
will be an intent so intense.
I used to move from one screen to another.
So from one activity to another.
But they are also useful in order to get something from the gallery.
So what we need to do is we need to prepare this intent and the intent will have an action pick.
So we are going to pick something.
And what are we going to pick?
Well, we're going to pick something from the media store, social media store images, dot media,
dot external content, urai.
So before I typed that out, I want to import the media store.
And then it will be a lot easier to type that, so external content you are, I guess, what I'm going
to need.
So this is the intent.
So I prepared the intent now.
And the next thing I can do is I can start an activity for assault, so start activity for result.
And what do I want to do here?
I want to pick my photo intent or run this intent on the gallery.
Now, what is this gallery?
Well, it's another constant that we need to create.
So I'd say we do that in our companion object.
So here I'm going to create another private constant value called gallery, which I'm going to assign
the value of to.
And maybe it's best if I use gallery of double here.
All right.
So now we have our gallery and it's used.
So it's used in here.
So what we're doing here is we are starting an intent and we started on the gallery and then we will
get a result, so we will know if that worked or not.
So let's go ahead and get that result.
And we do that with an override.
So there is a function called on activity result.
And just want to hear and what I want to do in here is I want to check if the result quote is that the
result was OK.
So here in the next line, I'm going to check if the result quote.
His activity, that result OK, as a considerable result, cancel results first, use the end result,
OK, and the result is the one we need and we need to import the activity.
So I'm going to import that.
And by the way, this result code is again coming from here from our own activity results.
So this all activity result, it has a similar structure as we've seen before.
Right.
With the on then keyboard result.
So let's look at where we had that before.
We had that on request permissions result.
So same structure we have on at the start, then the keyword and then result.
So that is the same structure here as well.
You see request code then.
Well, in this case it's permissions and then it's current result.
Now let's have a look at our other method that we created on activity results has also request code,
a result code in this case and the data of the intent.
So we have the result code now.
Now we can check if the request code of that is going to be the same as the one of our gallery.
So if the request code is equal, equal to our gallery.
Then we want to try and set the background of our image, so here, this background, I want to change
that image of you background or I want to replace the image and I want to replace it based on the result
of the activity we started.
So in here, I'm just going to say try because things could go wrong, right, because maybe the file
type doesn't fit or maybe the user just stopped at one point or whatever.
So I'm going to try it and I'm going to catch potential errors.
So how do we do that?
Well, we catch it with in this case, with a general exception here, e exception.
So a very generic exception and here we're just going to print the stack trace, this will just give
us in the log detail about the error that appeared.
But now what?
What do we want to try?
Well, we want to try to get the data.
So as you can see, we get requests called result code.
Now, what do we do with the data?
Well, let's have a look.
We checked the data because if we look at it, it's an nullable.
So data is of type nullable intent.
That data is not equal, not so this data here has a property called data, funnily enough, and we
can now check if it's null or not.
So only if it's not null, then we want to do something.
This basically means that if the data is not now, then the user has selected something and it worked.
So now we can go ahead and set the background of our image view.
Ivy, on the score background, first of all, is visibility to view data visible.
So, first of all, we're going to make the background visible if it wasn't before.
So really just making sure that it is visible.
And then we said that I have background that set image, you or I.
So we've seen this set image before, right.
Because there are multiple different set image functions here, set image drawable, resource bitmap
aykan and so forth.
But we're using and you arrive here because we are getting a your eye and where we're getting it from.
Well, we get it from the data.
So we just say data point that data.
So now, whatever the data or whatever the image is that was selected will be set as the background
image of our IV.
So basically we're saying, OK, we want to use to you.
Right, the.
Link on your device, so to speak, as the image and what we want to do if the data was empty.
So if the user didn't select any image, for example, or something else went wrong, well, then we
will just say with a little toast error in passing the image or is corrupted.
So maybe the image is incorrect, the wrong data type, or it's too huge or something like that.
And it's for some reason doesn't work on our phone, then we'll just display this error here.
All right, now that by itself will already work, but what we can also do is we can also get rid of
the background image that we had before, because if we look at our activity name and we look at the
image that is in the background, which should be at the very top in our frame layout.
OK, so we have this framework layout which was there for our image view to be just behind our drawing
view.
But in this image, we set a source right from the start.
So we set this drawable image right from the start, which is our Eiffel Tower.
OK, so this image here.
So now I'm going to get rid of that because I don't want that to constantly have a background.
Maybe the user just wants to have a white background.
Then this is going to be the default in this case.
And if the user wants to use another image, then he can select an image now.
OK, now let's test it.
Let's see if everything was set up correctly or if we're lacking anything there or missing something.
All right, the first thing we see, what definitely worked is that the image is now empty, so it's
no image in the background, so we can just draw on top of something.
Now, let's see if we can select an image.
All right.
And I'm using a download.
So I downloaded on the emulator of this image of Homer Simpson.
And I'm going to select this image and that we are.
So now we have it as a background.
And unfortunately, I already drew a little bit on top of it here and luckily enough, already with
yellow.
But now you can go ahead and draw on top of this image here.
And you have Homer Simpson there.
All right.
So that is how you can use permissions and how you can access images from the user and even set them
as the background image of, for example, our.
You hear where we can now start drawing Homer Simpson, just find a cool image that you want to have
as a background, and the best case is that you actually own it because if you don't own it, then you
should not shared on the Internet.
But if you own it, then you can, of course, share with us if you drew something pretty cool here,
OK?
So that was it.
Now you saw how you can do all of that beautiful stuff and we just have some more features left, because
one thing that is really a super important feature is to get rid of the first drawings that we did or
the first lines that we did.
So a back button would be super useful.
And you will learn how to use a back button in one of the next videos.
So I'd say, let's get to the next video.