1
00:00:00,430 --> 00:00:05,380
Welcome back and this video, we are going to combine several things that we have touched on in the

2
00:00:05,380 --> 00:00:13,450
last few videos, and we're going to see how we can now go ahead and use the camera in order to select

3
00:00:13,450 --> 00:00:20,700
an image or shoot an image first and then select that image in order to also display it as an image

4
00:00:20,710 --> 00:00:20,930
here.

5
00:00:21,160 --> 00:00:25,270
So pretty much get an image from the camera to our application.

6
00:00:25,610 --> 00:00:29,010
As you can see here now, the image is inside of this little image.

7
00:00:29,020 --> 00:00:34,600
You hereby we're not going to take care of the specifics of how to make the image of you beautiful and

8
00:00:34,600 --> 00:00:38,980
how to make the beautiful, but really just the functionality to start the intent to get the image and

9
00:00:38,980 --> 00:00:46,320
then to get the result of that activity, so to speak, to display this in our images.

10
00:00:46,720 --> 00:00:49,410
Therefore, I have an image of you here.

11
00:00:49,750 --> 00:00:56,590
You can set any settings you want, then a button which allows us to start the content in order to ask

12
00:00:56,590 --> 00:00:57,290
for permissions.

13
00:00:57,310 --> 00:01:04,269
And then, of course, if we have the permissions to start the activity, to display the camera application

14
00:01:04,269 --> 00:01:09,330
in order to then make an image and of course, then take that image and display the idea.

15
00:01:09,370 --> 00:01:13,730
So in order to do that, we need to prepare several things.

16
00:01:13,750 --> 00:01:16,180
So first of all, we need to have the permissions.

17
00:01:16,420 --> 00:01:23,790
Therefore, we can go to our manifest and here we need to use the permission for the camera.

18
00:01:23,800 --> 00:01:28,150
So we're just going to use Android permissions camera.

19
00:01:29,360 --> 00:01:34,040
All right, so this is the only permission that we're going to need in this example, and if you want

20
00:01:34,040 --> 00:01:37,110
to know more about permissions, then definitely check out the video on permissions.

21
00:01:37,130 --> 00:01:43,660
This is not specifically for that or this video here, because it's really just about the camera functionality.

22
00:01:44,000 --> 00:01:53,570
Then we need a companion object in which we store our variables, which are our constants, so to speak.

23
00:01:53,960 --> 00:02:01,190
So here the camera, a permission code will be one and then the camera value will be to.

24
00:02:02,600 --> 00:02:08,930
So one of them will be for the permission code, the other one will be for the intent and activity for

25
00:02:08,930 --> 00:02:09,370
result.

26
00:02:10,930 --> 00:02:13,420
That will make a lot more sense once we get into it.

27
00:02:13,660 --> 00:02:17,850
So first of all, we need, of course, our camera on Click Listener.

28
00:02:17,860 --> 00:02:18,970
So let's set that.

29
00:02:19,210 --> 00:02:23,640
And then the idea here is to, first of all, check if we have permissions.

30
00:02:24,190 --> 00:02:28,650
So that's generally always what we need to do if we want to use the camera.

31
00:02:28,660 --> 00:02:30,970
So check if we have permission.

32
00:02:30,970 --> 00:02:32,620
And which permission are we looking for?

33
00:02:32,650 --> 00:02:38,480
Well, specifically the camera permission and we check if the permission was granted.

34
00:02:39,040 --> 00:02:44,610
So if that's the case, then we want to use this camera functionality.

35
00:02:45,070 --> 00:02:47,590
So here comes in the interesting part.

36
00:02:47,600 --> 00:02:54,760
So we have this intent, which will be an intent that goes to and he wanted to import it real quick,

37
00:02:55,240 --> 00:03:06,370
which goes to the media store that action or it's just going to use this action image, capture functionality.

38
00:03:08,440 --> 00:03:14,440
So we prepared this intent to start our camera, pretty much that's what this does, all right.

39
00:03:14,440 --> 00:03:20,620
And then we can start the activity for result with the intent.

40
00:03:20,980 --> 00:03:22,690
And what shall we open?

41
00:03:22,720 --> 00:03:26,980
Well, we pass in the camera, which is our.

42
00:03:28,130 --> 00:03:30,310
It's variable that we created here.

43
00:03:30,380 --> 00:03:36,050
So this constant here, so we start the activity for result and as you know, when we do that, we need

44
00:03:36,050 --> 00:03:40,700
to pass in the intent that we want to start and then the code.

45
00:03:43,710 --> 00:03:53,220
To be more precise, the request code now, of course, you can call us one request coach if you want

46
00:03:53,220 --> 00:03:54,730
to be super clear about this.

47
00:03:55,320 --> 00:03:55,800
All right.

48
00:03:55,800 --> 00:04:01,050
So here we're passing the request code when starting the activity for a result.

49
00:04:01,060 --> 00:04:03,530
So we're pretty much waiting for a result.

50
00:04:04,200 --> 00:04:09,120
Now, of course, if we don't have permission, then we want to ask for permission.

51
00:04:09,120 --> 00:04:14,010
And I'm just going to use the standard format here, activity, combat request permissions.

52
00:04:14,340 --> 00:04:19,500
And then you need to pass an activity in which you want to request that the array of different permissions

53
00:04:19,500 --> 00:04:22,500
in our case is just one permission, which is a camera permission.

54
00:04:22,710 --> 00:04:27,180
And then we need to pass ENDA permission code as we prepared it up here.

55
00:04:29,060 --> 00:04:35,420
All right, now, of course, we need to do something in order to get the permissions running.

56
00:04:35,420 --> 00:04:37,670
So here on request.

57
00:04:40,090 --> 00:04:40,960
Permissions.

58
00:04:42,330 --> 00:04:42,990
Result.

59
00:04:44,150 --> 00:04:45,290
So this one here.

60
00:04:46,220 --> 00:04:48,800
It's going to be the one that we use in order to.

61
00:04:50,360 --> 00:04:51,390
Execute something.

62
00:04:51,410 --> 00:04:53,090
Once we get a permissions result.

63
00:04:53,120 --> 00:04:56,140
So either the user allowed it or he denied it.

64
00:04:58,020 --> 00:05:07,770
So I check if the permission request code, so request code is equal to the camera permission code.

65
00:05:10,350 --> 00:05:15,330
Because we're talking about emissions right here, here, the request code, if that's the same, then

66
00:05:15,930 --> 00:05:22,890
I want to check if the grant results.

67
00:05:24,330 --> 00:05:27,060
So this one here does a rate that we get here.

68
00:05:29,440 --> 00:05:31,330
As you can see, is an array of integers.

69
00:05:32,830 --> 00:05:38,470
If that is not empty and current results.

70
00:05:40,140 --> 00:05:41,370
At position zero.

71
00:05:42,940 --> 00:05:44,180
His permission granted.

72
00:05:44,530 --> 00:05:45,160
So here.

73
00:05:48,180 --> 00:05:53,160
Package manager, not permission granted.

74
00:05:55,500 --> 00:06:01,590
All right, so if permission is granted, then I want to pretty much do the same thing as we've done

75
00:06:01,590 --> 00:06:03,060
before with the intent.

76
00:06:03,060 --> 00:06:07,320
So here I want to start this activity for result here.

77
00:06:08,280 --> 00:06:10,740
So I'm going to paste that in here as well.

78
00:06:11,570 --> 00:06:11,970
Right.

79
00:06:11,970 --> 00:06:15,780
And otherwise, if we didn't get the grant result.

80
00:06:16,920 --> 00:06:19,620
So here in this health blog.

81
00:06:21,060 --> 00:06:26,430
I just want to write a little toast and say the permission was denied.

82
00:06:27,750 --> 00:06:32,640
So I'm just going to write something like you just denied the permission for camera.

83
00:06:33,030 --> 00:06:36,890
Don't worry, can out in the settings, OK?

84
00:06:38,000 --> 00:06:43,300
So this is the on request permission results, so this is, again, nothing new if you watched the permissions

85
00:06:43,310 --> 00:06:43,730
video.

86
00:06:44,180 --> 00:06:50,330
Now comes the Newport, the interesting part when it comes to the camera, because now we combine what

87
00:06:50,330 --> 00:06:53,720
we have seen with activity for result.

88
00:06:54,610 --> 00:06:55,150
And.

89
00:06:56,360 --> 00:07:04,790
The usage of cameras, because what we get from an activity for result is on activity result.

90
00:07:05,300 --> 00:07:10,100
So this function here is automatically then called once we get the results.

91
00:07:10,120 --> 00:07:13,280
So once the user, for example, selected an image with a camera.

92
00:07:14,930 --> 00:07:18,080
So now we can go ahead and check if the result code.

93
00:07:19,940 --> 00:07:23,820
It's going to be activity result.

94
00:07:24,140 --> 00:07:24,770
OK.

95
00:07:27,090 --> 00:07:35,610
And why we need to import activity and if that's the case, then we can check if the request code is

96
00:07:35,610 --> 00:07:37,410
equal to our camera request code.

97
00:07:39,010 --> 00:07:42,520
So here, camera request Kote.

98
00:07:44,960 --> 00:07:52,700
Because if that's the case, then we can display the data that we are oppressed from an activity result

99
00:07:53,330 --> 00:08:01,680
into our live image, which is and our activity in this image view, which was called IVI image.

100
00:08:01,700 --> 00:08:07,790
So then we can set that image view to be the image that we have just received as the result of an activity

101
00:08:07,790 --> 00:08:12,460
result, which pretty much as a result on start activity for yourself.

102
00:08:14,040 --> 00:08:16,890
OK, so now we can go ahead and hear.

103
00:08:18,500 --> 00:08:27,290
Get the data, and I'm saving that in a thumbnail, which is of type bitmap, and we get that need to

104
00:08:27,290 --> 00:08:30,380
import it real quick, then we get that from data.

105
00:08:32,080 --> 00:08:33,520
That extra us.

106
00:08:36,659 --> 00:08:37,710
So here, Daito.

107
00:08:38,789 --> 00:08:47,230
That was passed has some extras, and from there we want to get that data.

108
00:08:47,280 --> 00:08:55,260
So it's called data as a bitmap so the data can be in many different shapes and forms, but we want

109
00:08:55,260 --> 00:08:56,550
to get it as a bitmap.

110
00:08:56,850 --> 00:09:02,180
Now, the thing is, data is a nullable and extras is a nullable as well.

111
00:09:02,190 --> 00:09:10,290
So we need to focus on Revit here and then we have the thumbnail now we can go ahead and use that.

112
00:09:10,350 --> 00:09:12,140
So our image view.

113
00:09:12,150 --> 00:09:21,690
So I've image set, image bitmap can be our thumbnail because our thumbnail is.

114
00:09:23,040 --> 00:09:25,050
A bitmap and here, of course.

115
00:09:26,550 --> 00:09:28,380
Writing it correctly will be a little better.

116
00:09:29,310 --> 00:09:35,550
So here we set the image of the image for you to be, the thumbnail that we get from the data that was

117
00:09:35,550 --> 00:09:38,700
passed to us from the productivity result.

118
00:09:39,570 --> 00:09:42,780
So pretty much when we press on this button.

119
00:09:46,130 --> 00:09:48,770
Then we get to the camera, so that is.

120
00:09:50,310 --> 00:09:56,400
Here, set on Click Listener, so we click on the button it checks, do we have permission?

121
00:09:56,760 --> 00:10:02,610
If we have permission, which we do, then start a certain Internet media store action image capture,

122
00:10:03,120 --> 00:10:10,380
which is this screen here that it will be started as soon as this start activity result is.

123
00:10:11,800 --> 00:10:12,550
Executed.

124
00:10:13,100 --> 00:10:16,840
OK, so then and waits for the result.

125
00:10:17,820 --> 00:10:27,030
And once I select an image here, I press this button, then it goes back to this activity that we are

126
00:10:27,030 --> 00:10:27,330
at.

127
00:10:27,510 --> 00:10:34,750
So it goes back to our main activity and this one activity result is executed.

128
00:10:35,370 --> 00:10:43,560
So then it checks as the result code of result code, OK, which means that the selection work out.

129
00:10:44,100 --> 00:10:50,850
And then was the request code that got you that activity result, was it the common request code?

130
00:10:51,750 --> 00:10:53,970
So what do we get the camera requests from?

131
00:10:54,000 --> 00:10:58,200
Well, the thing is, we passed it here when we started the activity for results.

132
00:10:58,200 --> 00:11:01,170
So you can see your intent with the camera request code.

133
00:11:01,980 --> 00:11:05,490
And that is, of course, our constant that we have created here.

134
00:11:06,280 --> 00:11:11,370
OK, so we just use it as a concept because we want to use it multiple times throughout the application

135
00:11:11,370 --> 00:11:13,860
or potentially use it throughout the application.

136
00:11:13,860 --> 00:11:17,340
And it's just good practice to store it in a companion object.

137
00:11:18,950 --> 00:11:25,710
And then we can, of course, execute whatever we want to do if the result came back.

138
00:11:26,080 --> 00:11:33,440
Now the thing is the result is of type data, but in this case, the data that we get is actually going

139
00:11:33,440 --> 00:11:35,060
to be a bitmap.

140
00:11:35,270 --> 00:11:43,580
So now we can take that bitmap and store it as a bit more variable and then set our image for you to

141
00:11:43,580 --> 00:11:44,480
be that bitmap.

142
00:11:46,660 --> 00:11:47,220
All right.

143
00:11:47,530 --> 00:11:54,950
And similarly, other selections work as well, or other activity for result options work as well.

144
00:11:55,360 --> 00:11:59,800
So here you can, of course, use other types of intense.

145
00:11:59,800 --> 00:12:05,650
It could be an intent that we will use later on where we start an activity that we don't develop ourselves.

146
00:12:05,800 --> 00:12:10,800
And this intent could go to third party activities as well.

147
00:12:10,840 --> 00:12:16,270
For example, we are going to use places later on, which is from Google, where you can enter a name

148
00:12:16,270 --> 00:12:20,070
of a place and it will display that place on the map.

149
00:12:20,620 --> 00:12:25,840
And we want to get the information from the place, for example, the latitude and longitude, and we

150
00:12:25,840 --> 00:12:28,590
want to get that, of course, in the activity that started the intent.

151
00:12:28,900 --> 00:12:33,360
So we are also going to use the same approach, which is start activity for result.

152
00:12:33,870 --> 00:12:34,350
All right.

153
00:12:34,360 --> 00:12:35,880
So these are the basics behind it.

154
00:12:35,890 --> 00:12:42,160
And of course, we are going to look into that again and again, of course, each time when we use that.

155
00:12:42,160 --> 00:12:43,530
And we're going to use it quite a lot.

156
00:12:43,540 --> 00:12:46,660
So no worries if that was a little bit too much now.

157
00:12:47,290 --> 00:12:48,970
And see you in the next video.

