1
00:00:00,820 --> 00:00:01,420
Welcome back.

2
00:00:01,720 --> 00:00:06,280
And this video, we're going to implement the functionality so that we can actually click on this button

3
00:00:06,280 --> 00:00:08,950
and then go to the gallery and select an image from there.

4
00:00:09,370 --> 00:00:19,660
So therefore, inside of my request permission, I'm going to start an intent once I have granted the

5
00:00:19,660 --> 00:00:22,300
permission, so once the permission was granted to me.

6
00:00:22,600 --> 00:00:28,060
So if is granted the permission that we're looking at and that is our gallery permission, if that's

7
00:00:28,060 --> 00:00:35,290
the case, then let's go ahead and pick an intent or create a new intent that will be called content.

8
00:00:35,680 --> 00:00:37,030
And this will just be an intent.

9
00:00:37,030 --> 00:00:42,100
So we have seen how to use contents before, right where we can move from one activity to another inside

10
00:00:42,100 --> 00:00:43,090
of our application.

11
00:00:43,330 --> 00:00:48,610
But you can also run intents in order to go to other applications.

12
00:00:48,970 --> 00:00:50,400
And in this case, to the gallery.

13
00:00:50,410 --> 00:00:53,020
So here we can use action.

14
00:00:53,660 --> 00:00:59,800
I pick this one action pick and then here we go to the media store.

15
00:00:59,890 --> 00:01:09,190
So we just say, I want to go to the media store, dot images, thoughts, media thoughts, external

16
00:01:09,190 --> 00:01:09,880
content.

17
00:01:09,880 --> 00:01:12,760
You are and let's import media store here and here.

18
00:01:12,760 --> 00:01:14,500
It's external content you are.

19
00:01:15,460 --> 00:01:18,550
OK, so this will be the intent that I need to create.

20
00:01:18,580 --> 00:01:20,090
So this will be the intent.

21
00:01:20,470 --> 00:01:24,880
And now we just need to find a way to start this intent.

22
00:01:25,120 --> 00:01:27,460
So just starting this content will not be enough.

23
00:01:27,850 --> 00:01:35,620
What we actually want to do is we want to be able to select the image and therefore we need to create

24
00:01:35,980 --> 00:01:37,060
a launcher.

25
00:01:37,450 --> 00:01:44,530
And how that works with the latest Android Studio version is by creating a activity result launcher

26
00:01:44,530 --> 00:01:45,030
once again.

27
00:01:45,040 --> 00:01:48,190
So the same what we have done with the request permission.

28
00:01:48,530 --> 00:01:50,450
But this time we're just opening something else.

29
00:01:50,470 --> 00:01:56,920
So here I'm just going to call this one open gallery launcher, which will be an activity result launcher

30
00:01:57,700 --> 00:01:59,800
and this time of type intent.

31
00:02:01,420 --> 00:02:06,490
And we also need to register for activity result here.

32
00:02:06,880 --> 00:02:15,580
But this time I'm passing a contract that will not be a multiple request permission, but a start activity

33
00:02:15,580 --> 00:02:16,150
for results.

34
00:02:16,840 --> 00:02:21,490
So here start activity for results like so OK.

35
00:02:21,490 --> 00:02:29,560
And this activity for result is a method that we need to execute here and then we can get the result

36
00:02:29,560 --> 00:02:31,810
from that start activity for result request.

37
00:02:32,230 --> 00:02:36,520
So here we just get the result and we can now work with it.

38
00:02:36,850 --> 00:02:43,890
OK, so this lambda basically this part here register for activity result allows us to execute.

39
00:02:43,900 --> 00:02:49,600
Why won't you run in the case of the result that we're given and it gives us the result at the same

40
00:02:49,600 --> 00:02:49,930
time?

41
00:02:50,650 --> 00:02:55,660
OK, so here what we can now do is we can check what kind of result that was.

42
00:02:55,750 --> 00:03:00,420
So if the result, that result code was result?

43
00:03:00,460 --> 00:03:00,970
OK.

44
00:03:01,360 --> 00:03:11,500
So and also the result data isn't empty, so is not null, then I want to run some code.

45
00:03:12,220 --> 00:03:13,780
So what is it that I want to run?

46
00:03:13,990 --> 00:03:18,100
Well, I want to take that data that is given to me from that result.

47
00:03:18,730 --> 00:03:22,000
And then I want to assign it to my image of you.

48
00:03:22,780 --> 00:03:25,630
So what is the image that I want to assign it to?

49
00:03:25,780 --> 00:03:26,950
Well, let's look at it.

50
00:03:27,100 --> 00:03:29,050
Let's go over to our activity.

51
00:03:29,050 --> 00:03:29,920
Main example.

52
00:03:30,430 --> 00:03:35,080
And in here we have this image background.

53
00:03:37,060 --> 00:03:37,750
What is it?

54
00:03:38,320 --> 00:03:39,370
I think it's.

55
00:03:40,810 --> 00:03:42,400
For our frame layout here.

56
00:03:43,150 --> 00:03:51,340
So we have this Ivy background, that's our image view and that is currently this image here, and I

57
00:03:51,340 --> 00:03:54,340
want to replace that with an image that we select from the gallery, right?

58
00:03:54,940 --> 00:04:03,160
So in order to do that, what we need to do is, of course, to create a image background or an image

59
00:04:03,160 --> 00:04:07,480
view here, find it by its ID.

60
00:04:08,470 --> 00:04:12,070
And it will be brought ID Dot I've backgrounds.

61
00:04:12,670 --> 00:04:16,300
And of course, I need to import image view here for this to work.

62
00:04:16,660 --> 00:04:19,300
So now we have access to the background image.

63
00:04:19,930 --> 00:04:22,960
So to this image view and we can now assign.

64
00:04:25,180 --> 00:04:26,840
Data to it, so here.

65
00:04:26,860 --> 00:04:29,210
Image, background, thought, set image.

66
00:04:29,470 --> 00:04:33,490
And here you see we can draw both a resource, a bitmap and so forth.

67
00:04:33,700 --> 00:04:37,300
But what we are getting is going to be a new array.

68
00:04:37,990 --> 00:04:39,250
So let's use that.

69
00:04:39,250 --> 00:04:39,890
You are I.

70
00:04:40,270 --> 00:04:46,090
Because if you look at it, this media store image media data is external content.

71
00:04:46,090 --> 00:04:53,260
You are a your eye is basically the location on your device, so it's like a path on your browser.

72
00:04:53,650 --> 00:05:00,760
So here, for example, I have this path and that is where it is on my PC, right?

73
00:05:00,820 --> 00:05:07,810
So users admin, desktop and road projects and you have the same thing with your images on your Android

74
00:05:07,810 --> 00:05:08,120
device.

75
00:05:08,120 --> 00:05:12,580
So there's also there's content your eye, which is basically this link that I just showed you.

76
00:05:12,610 --> 00:05:14,260
So the location on the device.

77
00:05:14,710 --> 00:05:19,270
And that's what we're getting and we're getting it from this intent.

78
00:05:19,900 --> 00:05:23,740
Well, basically, we're getting it from the activity for result.

79
00:05:24,070 --> 00:05:24,410
OK.

80
00:05:24,430 --> 00:05:33,220
The result gives us this data and we can access it by using result data, which is invaluable.

81
00:05:33,400 --> 00:05:34,240
You see, here we are.

82
00:05:34,510 --> 00:05:36,250
We checked whether it was now.

83
00:05:36,700 --> 00:05:40,840
So it is another tool that can be now thought data.

84
00:05:41,380 --> 00:05:43,860
So this will then give us the actual data.

85
00:05:43,870 --> 00:05:45,100
So the actual image.

86
00:05:45,610 --> 00:05:49,150
So the result data's location status data.

87
00:05:50,430 --> 00:05:50,850
OK.

88
00:05:53,190 --> 00:06:03,840
So now we can use this open gallery launcher inside of our is granted here, so if we were granted permission

89
00:06:03,840 --> 00:06:09,810
to access the gallery, we can try to access it by launching our.

90
00:06:11,640 --> 00:06:14,550
Open gallery launcher with our intent.

91
00:06:15,630 --> 00:06:23,040
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

92
00:06:23,040 --> 00:06:24,360
of Android Studio.

93
00:06:24,540 --> 00:06:25,490
I'm not a big fan of it.

94
00:06:25,500 --> 00:06:29,550
I think the older version was a lot easier with starting activity for results and stuff.

95
00:06:29,850 --> 00:06:34,290
But here's what you need to do is you need to have a activity result launcher.

96
00:06:34,620 --> 00:06:36,870
You need to define what kind of launch it will be.

97
00:06:36,870 --> 00:06:42,960
In my case, it will be based on an intent to register for activity results so that you can actually

98
00:06:43,230 --> 00:06:44,400
wait for a result.

99
00:06:44,400 --> 00:06:49,470
You need to use the activity result contracts with that activity for results and then you actually get

100
00:06:49,470 --> 00:06:52,110
the result and you can check the result code.

101
00:06:52,380 --> 00:06:58,710
If it was OK, so we got data back and if it's not empty, let's assign it to the image background.

102
00:06:58,710 --> 00:07:01,920
So the data that we're getting, which is just the location of the data.

103
00:07:01,920 --> 00:07:07,890
So we're not really getting the data, but we're actually getting the location of the data so that we

104
00:07:07,890 --> 00:07:09,900
can just use the location.

105
00:07:09,900 --> 00:07:12,930
So we're not copying the image into our application.

106
00:07:13,170 --> 00:07:17,010
We're just setting the image based on where it is on your device.

107
00:07:17,340 --> 00:07:22,410
OK, because otherwise you would duplicate data and you would create more data on your phone and it

108
00:07:22,410 --> 00:07:24,210
would store it somewhere and so forth.

109
00:07:24,390 --> 00:07:29,010
So the smart approach here is really just to say, OK, where is this image?

110
00:07:29,310 --> 00:07:30,570
Let's just use it from there.

111
00:07:30,840 --> 00:07:33,530
And yeah, let's just use that as a background.

112
00:07:33,540 --> 00:07:38,460
So here we are, of course, creating the find if you by the reference, so that we can actually actually

113
00:07:38,460 --> 00:07:41,730
access to some L image view that we created.

114
00:07:42,420 --> 00:07:42,840
OK.

115
00:07:43,290 --> 00:07:46,010
So at this point, let's check it out.

116
00:07:46,020 --> 00:07:49,290
Let's see if the button will do something.

117
00:07:50,320 --> 00:07:58,660
And we'll start the gallery in, so basically move to the gallery activity slash application.

118
00:07:59,050 --> 00:08:02,980
So let's click on it and we see we were granted the permission.

119
00:08:02,980 --> 00:08:10,960
So I don't have a photo here, so let's download a photo and let's just add no account.

120
00:08:11,170 --> 00:08:15,250
So if you don't have photos on your device, you can just search for photos here.

121
00:08:15,340 --> 00:08:17,440
I like to use Homer Simpson.

122
00:08:18,070 --> 00:08:19,510
The good old Simpson.

123
00:08:19,990 --> 00:08:24,070
And then you see, it's all beautifully written in German.

124
00:08:25,400 --> 00:08:26,240
I accept.

125
00:08:26,540 --> 00:08:32,990
And now let's use the doughnut eating Homer Simpson, so click on it or hold press.

126
00:08:33,200 --> 00:08:36,320
Download the image and download it.

127
00:08:38,200 --> 00:08:39,130
OK, now.

128
00:08:40,350 --> 00:08:45,900
Let's go back to our application, select a photo inside of downloads, you can see we have Homer Simpson

129
00:08:46,260 --> 00:08:48,090
and now we can start drawing Homer.

130
00:08:49,140 --> 00:08:56,970
Let's use the fine color here, and you see we can now start drawing this beautiful guy with his beautiful

131
00:08:56,970 --> 00:08:58,590
head and stuff like that.

132
00:08:59,310 --> 00:09:08,550
OK, so generally you now have an idea, I guess, of how you can get access to.

133
00:09:09,830 --> 00:09:10,610
Your gallery.

134
00:09:11,000 --> 00:09:15,110
So let me draw this beautiful homer here, and you see amazing.

135
00:09:16,550 --> 00:09:18,980
I should have become an artist, not.

136
00:09:20,910 --> 00:09:21,510
There we are.

137
00:09:21,540 --> 00:09:22,440
Beautiful, Homer.

138
00:09:22,650 --> 00:09:24,090
He's super high on doughnuts.

139
00:09:25,200 --> 00:09:30,990
OK, so that's it for this video now you know how to select images.

140
00:09:30,990 --> 00:09:33,690
And of course, you can now add more images.

141
00:09:33,690 --> 00:09:36,880
You can, I think, even go to the camera app.

142
00:09:36,910 --> 00:09:37,920
Let's see if it works.

143
00:09:38,670 --> 00:09:42,060
And it will go to well.

144
00:09:42,060 --> 00:09:44,310
I don't want to allow to do that.

145
00:09:44,400 --> 00:09:47,700
Let's make an image or take an image.

146
00:09:48,150 --> 00:09:48,830
Let's go back.

147
00:09:48,840 --> 00:09:50,490
Let's select an image you can see.

148
00:09:50,490 --> 00:09:55,550
We can now select the image from our picture, and you see what we had in the background was safe.

149
00:09:55,560 --> 00:09:57,840
So I still have parts of Homer Simpson here.

150
00:09:59,360 --> 00:10:01,670
And in the background, we have the room.

151
00:10:02,180 --> 00:10:03,740
OK, that's it for this video.

152
00:10:03,950 --> 00:10:11,150
See you in the next one where we are going to add an undue button functionality because while I'm drawing

153
00:10:11,150 --> 00:10:14,990
something right and then I want to undo it because it just doesn't fit.

154
00:10:15,350 --> 00:10:17,600
And that's really something that we need to implement.

155
00:10:17,840 --> 00:10:18,620
So see you there.

