1
00:00:00,750 --> 00:00:05,430
So now let's actually run our project and see what happens.

2
00:00:05,430 --> 00:00:10,850
So as you can see here our application is actually running on this emulator here.

3
00:00:11,040 --> 00:00:11,730
Okay.

4
00:00:12,090 --> 00:00:15,050
And now we can't we can see these images here.

5
00:00:15,090 --> 00:00:15,620
OK.

6
00:00:15,750 --> 00:00:23,250
If I scroll down I can see these images but my application is actually taking too much memory because

7
00:00:23,250 --> 00:00:28,300
of these images that I actually assigned to these image views here.

8
00:00:28,380 --> 00:00:31,550
As you can see here and it's crashed.

9
00:00:31,620 --> 00:00:37,730
So as you can see it's actually to go inside this log here in order to see the cause of this error.

10
00:00:37,980 --> 00:00:43,600
And it says that's failed as he has it's Java that land that out of memory error.

11
00:00:43,640 --> 00:00:52,980
Ok failed to allocate this value by its location with this value of free bytes and eight megabyte until

12
00:00:53,730 --> 00:00:59,400
this value as you can see here is the cause of this error is that our application is taking too much

13
00:00:59,400 --> 00:01:06,580
memory because of those high quality images and we cannot just type I animals animal that image resource.

14
00:01:06,600 --> 00:01:10,590
And the name of that image we need to actually do something with those images.

15
00:01:10,630 --> 00:01:13,930
OK you need to actually reduce the size of those images.

16
00:01:13,950 --> 00:01:15,080
So how we can do that.

17
00:01:15,150 --> 00:01:20,810
So in order to do that let's actually that its design code here and now and when I actually pasting

18
00:01:20,850 --> 00:01:24,290
some lines of code here and then I'm going to explain to you what's going on here.

19
00:01:24,300 --> 00:01:25,000
OK everybody.

20
00:01:25,020 --> 00:01:29,490
Now I'm going to actually paste in some lines of code here and then I'm going to explain to you what's

21
00:01:29,490 --> 00:01:30,820
going on here.

22
00:01:30,840 --> 00:01:34,860
So as you can see here first about in line are fifty one.

23
00:01:34,980 --> 00:01:38,940
I actually created a bitmap factory that options object.

24
00:01:38,940 --> 00:01:39,540
OK.

25
00:01:39,930 --> 00:01:42,270
So what is actually a bitmap.

26
00:01:42,300 --> 00:01:48,300
Let's actually go to the Android documentation in order to see what's what what are bitmaps as you can

27
00:01:48,300 --> 00:01:56,160
see here it says bitmaps bitmap objects innovate that keeps your user interface components responsive

28
00:01:56,610 --> 00:02:02,320
and avoids exceeding your application memory really be ok if you are not careful.

29
00:02:02,430 --> 00:02:08,400
Bitmaps can quickly consume your available memory budget leading to an application crash due to the

30
00:02:08,520 --> 00:02:10,480
dreaded exception OK.

31
00:02:10,530 --> 00:02:14,570
Mobile devices typically have constrained system resources.

32
00:02:14,580 --> 00:02:15,020
OK.

33
00:02:15,180 --> 00:02:22,050
Android devices can have as little as 16 megabytes of memory available to a single application.

34
00:02:22,200 --> 00:02:23,640
OK and this is very important.

35
00:02:23,640 --> 00:02:30,590
OK and bitmaps as you can see here take up a lot of memory especially for rich images like photographs.

36
00:02:30,600 --> 00:02:32,500
OK on here.

37
00:02:32,640 --> 00:02:34,570
Let's actually go click on this.

38
00:02:34,580 --> 00:02:37,850
Lot loading large bitmaps efficiently.

39
00:02:37,890 --> 00:02:40,890
So I already opened that here in a new tab.

40
00:02:40,950 --> 00:02:44,990
As you can see here it says images come in all shapes and sizes.

41
00:02:45,240 --> 00:02:49,680
In many cases they are larger than required for a typical application user interface.

42
00:02:49,680 --> 00:02:56,580
Ok for example the system Gallerie application displays photos taken using your Andray devices camera

43
00:02:56,610 --> 00:03:01,080
which are typically much higher resolution than the screen density of your device.

44
00:03:01,090 --> 00:03:05,050
OK and here it says Arete bitmap dimensions and type.

45
00:03:05,350 --> 00:03:09,270
OK we can actually read the bitmap dimensions and type.

46
00:03:09,280 --> 00:03:10,380
Both are here.

47
00:03:10,380 --> 00:03:12,560
This is very important as you can see here.

48
00:03:12,780 --> 00:03:15,380
I skate down version into memory.

49
00:03:15,430 --> 00:03:20,680
Ok so here as you can see it says now that the image dimensions are known.

50
00:03:20,790 --> 00:03:27,360
They can be used to decide if the full image should be loaded into memory or if a sub sampled version

51
00:03:27,510 --> 00:03:29,100
should be loaded instead.

52
00:03:29,100 --> 00:03:31,000
Here are some factors to consider.

53
00:03:31,020 --> 00:03:36,660
Estimated memory usage of loading the full image in memory amount of memory you are willing to commit

54
00:03:37,170 --> 00:03:42,360
to loading this image given any other memory requirements of your application.

55
00:03:42,360 --> 00:03:48,020
Dimensions of the target image view or the UAE component that the image is to be loaded into.

56
00:03:48,030 --> 00:03:53,880
So as you can see here the application or the dimensions of image view.

57
00:03:53,910 --> 00:03:55,950
So let's actually find this clear case here.

58
00:03:56,040 --> 00:04:01,470
As you can see is for example for the layout which I assign the value 100 and for the layout height

59
00:04:01,500 --> 00:04:03,650
I again assign the value 100.

60
00:04:03,670 --> 00:04:08,450
OK so these are the important factors that we need to consider.

61
00:04:08,520 --> 00:04:13,590
For anyone to actually output some images to the screen and as you can see here it says for example

62
00:04:13,800 --> 00:04:17,540
it's not warf loading our value pixel image.

63
00:04:17,550 --> 00:04:24,240
As you can see here disvalue pics that image into memory if it will eventually be displayed in disvalue

64
00:04:24,240 --> 00:04:26,530
pixel Formanek Farmdale in an image view.

65
00:04:26,580 --> 00:04:34,410
OK to tell the decoder to subsample the image loading a smaller version into memory sets in sample size.

66
00:04:34,410 --> 00:04:37,680
So this value is valuable.

67
00:04:37,680 --> 00:04:40,000
We need to set this variable to true.

68
00:04:40,310 --> 00:04:43,990
OK in your math factory that options object.

69
00:04:44,050 --> 00:04:45,790
OK for example.

70
00:04:46,320 --> 00:04:54,330
As you can see this is an example for this it bitmap OK but here in our application in a customized

71
00:04:54,350 --> 00:04:55,600
Lisp view as you can see.

72
00:04:55,860 --> 00:04:58,230
I created my file through the options.

73
00:04:58,260 --> 00:05:02,640
OK so that they can actually call this valuable on this object.

74
00:05:02,680 --> 00:05:03,130
OK.

75
00:05:03,260 --> 00:05:05,370
Options start in sample size four.

76
00:05:05,710 --> 00:05:06,370
OK.

77
00:05:06,650 --> 00:05:09,440
And here I created an object of type bitmap.

78
00:05:09,620 --> 00:05:10,210
OK.

79
00:05:10,370 --> 00:05:12,810
Bitmapped factory that the resource.

80
00:05:12,860 --> 00:05:13,440
OK.

81
00:05:13,640 --> 00:05:21,920
So because of you actually have more images in inside of the project as you can see here.

82
00:05:22,140 --> 00:05:25,700
We have these images inside of this adorable folder in our project.

83
00:05:25,700 --> 00:05:31,070
So if we need we needed to actually call this D-CT resource here and on our factory.

84
00:05:31,140 --> 00:05:31,580
OK.

85
00:05:31,730 --> 00:05:34,390
So bitmapped factor that later D-CT resource.

86
00:05:34,400 --> 00:05:39,580
So and this it actually accepts some arguments context that gets resources.

87
00:05:39,680 --> 00:05:46,470
So here we need to actually refers to the resources of our application so context that gets resources.

88
00:05:46,490 --> 00:05:51,250
And the second argument is the specific image state of our animal images.

89
00:05:51,270 --> 00:05:57,050
Ari Okay and here as you can see we have another argument here options.

90
00:05:57,200 --> 00:06:03,080
So these options of with scientists in sample size here as you can see disvalue for our image.

91
00:06:03,470 --> 00:06:07,880
And here I am assigning this bitmap to to my IMJ animal image.

92
00:06:07,940 --> 00:06:08,570
OK.

93
00:06:08,930 --> 00:06:11,780
So let's actually run our project and see what happens.

94
00:06:11,900 --> 00:06:19,480
So now as you can see of we have now this came down version of our images on these images views.

95
00:06:19,580 --> 00:06:24,420
And as you can see the quality of our images are actually a little bit low.

96
00:06:24,500 --> 00:06:26,350
OK low quality images.

97
00:06:26,480 --> 00:06:30,190
And let's actually change this number four to number eight.

98
00:06:30,470 --> 00:06:32,220
And the search you did on our project again.

99
00:06:32,240 --> 00:06:37,460
So as you can see here again our application is running on this emulator and you can see that these

100
00:06:37,760 --> 00:06:42,500
images are actually very low quality OK low quality images.

101
00:06:42,500 --> 00:06:44,200
So let's actually change this.

102
00:06:44,240 --> 00:06:51,500
And as you can see here although our images are actually a little bit low quality but they're not actually

103
00:06:51,500 --> 00:06:55,850
taking too much memory so that our application actually doesn't crash.

104
00:06:55,910 --> 00:06:56,870
As you can see here.

105
00:06:57,070 --> 00:06:57,670
OK.

106
00:06:58,010 --> 00:07:04,040
So if we for example change disvalue to number two are the quality of our images are going to be a little

107
00:07:04,040 --> 00:07:04,600
bit high.

108
00:07:04,610 --> 00:07:06,140
High quality images.

109
00:07:06,140 --> 00:07:11,880
So now that's actually on our project again and here as you can see now we have high quality images.

110
00:07:11,900 --> 00:07:12,580
Okay.

111
00:07:12,970 --> 00:07:18,740
But I think this is not the right way to write our coats but here I just wanted to show you what is

112
00:07:18,740 --> 00:07:21,110
this a bit more effectively that options.

113
00:07:21,150 --> 00:07:26,660
OK how and how we can actually use the bitmap factory in order to actually see the images and output

114
00:07:26,660 --> 00:07:27,950
those images to the screen.

115
00:07:27,950 --> 00:07:28,500
OK.

116
00:07:28,760 --> 00:07:32,440
So let's actually comment out these lines of code here.

117
00:07:32,600 --> 00:07:33,450
OK.

118
00:07:33,860 --> 00:07:42,530
And now according to the documentations we can actually use these if it's OK that the Android gives

119
00:07:42,530 --> 00:07:48,090
us the Android documentation gives us as you can see we can't use this method so I just copied the Smurfette

120
00:07:48,110 --> 00:07:49,130
here as you can see.

121
00:07:49,130 --> 00:07:54,980
Now let's actually go to an android studio and after the end in curly brace of this method as you can

122
00:07:54,980 --> 00:07:58,880
see of this get View method I want to paste this method here and then I'm going to explain to you what's

123
00:07:58,880 --> 00:08:00,350
going on here.

124
00:08:00,380 --> 00:08:02,390
So as you can see here this is our method.

125
00:08:02,570 --> 00:08:03,680
OK.

126
00:08:03,740 --> 00:08:08,980
And as you can see the Axis modifier of this and if it is public this method is static.

127
00:08:08,990 --> 00:08:14,840
So it is going to be a class MF'er we can use we can call this an effect on our class and the return

128
00:08:14,840 --> 00:08:19,360
type of this if it is it and the name of this method is calculate in sample size.

129
00:08:19,460 --> 00:08:19,960
OK.

130
00:08:20,060 --> 00:08:26,690
So it's going I should calculate this impulse in sample size variable for us automatically according

131
00:08:26,690 --> 00:08:27,520
to our images.

132
00:08:27,530 --> 00:08:28,440
OK.

133
00:08:28,670 --> 00:08:34,050
So this Biffen actually accepts some arguments between bitmapped fancy options options.

134
00:08:34,130 --> 00:08:34,800
OK.

135
00:08:35,050 --> 00:08:36,440
It requires.

136
00:08:36,830 --> 00:08:38,540
It requires height.

137
00:08:38,720 --> 00:08:39,350
OK.

138
00:08:39,590 --> 00:08:45,740
And as you can see here inside of this it actually created some variables local variables.

139
00:08:45,740 --> 00:08:52,310
The first one is constant int height as you can see and hear it put an assignment operator and then

140
00:08:52,310 --> 00:08:55,740
it referred to this options argument that's outright.

141
00:08:55,740 --> 00:09:00,410
So out how outside is going to actually give us the height of our image.

142
00:09:00,480 --> 00:09:01,120
Okay.

143
00:09:01,580 --> 00:09:03,230
And then here we have a V.

144
00:09:03,330 --> 00:09:03,910
Okay.

145
00:09:04,130 --> 00:09:08,840
And then it actually gives us the outwits for key the Viets of our image.

146
00:09:08,870 --> 00:09:10,440
And then it's going to say that's why you do this.

147
00:09:10,470 --> 00:09:11,390
It's constant.

148
00:09:11,420 --> 00:09:12,030
OK.

149
00:09:12,410 --> 00:09:16,080
And we have these int in sample size variable.

150
00:09:16,160 --> 00:09:16,750
OK.

151
00:09:16,790 --> 00:09:22,420
And it assigned a value in number one to this variable and it actually created an if statement here

152
00:09:22,430 --> 00:09:29,090
as you can see if the value of this height as you can see here this local variable height this constant

153
00:09:29,330 --> 00:09:33,770
is greater than the value of these required height that we have here.

154
00:09:34,010 --> 00:09:42,920
Or if the value of these v as you can see here is greater than the value of these require Veach as you

155
00:09:42,920 --> 00:09:47,950
can see here then it's going to actually execute these lines of code inside of these if statement.

156
00:09:47,960 --> 00:09:48,700
OK.

157
00:09:49,220 --> 00:09:51,920
So as you can see here it's actually created a comment for us.

158
00:09:51,920 --> 00:09:59,200
It says calculate the largest sample size value that is a power of two and keeps both height and weight

159
00:09:59,300 --> 00:10:01,970
larger request that height and if it's OK.

