So now let's actually run our project and see what happens.
So as you can see here our application is actually running on this emulator here.
Okay.
And now we can't we can see these images here.
OK.
If I scroll down I can see these images but my application is actually taking too much memory because
of these images that I actually assigned to these image views here.
As you can see here and it's crashed.
So as you can see it's actually to go inside this log here in order to see the cause of this error.
And it says that's failed as he has it's Java that land that out of memory error.
Ok failed to allocate this value by its location with this value of free bytes and eight megabyte until
this value as you can see here is the cause of this error is that our application is taking too much
memory because of those high quality images and we cannot just type I animals animal that image resource.
And the name of that image we need to actually do something with those images.
OK you need to actually reduce the size of those images.
So how we can do that.
So in order to do that let's actually that its design code here and now and when I actually pasting
some lines of code here and then I'm going to explain to you what's going on here.
OK everybody.
Now I'm going to actually paste in some lines of code here and then I'm going to explain to you what's
going on here.
So as you can see here first about in line are fifty one.
I actually created a bitmap factory that options object.
OK.
So what is actually a bitmap.
Let's actually go to the Android documentation in order to see what's what what are bitmaps as you can
see here it says bitmaps bitmap objects innovate that keeps your user interface components responsive
and avoids exceeding your application memory really be ok if you are not careful.
Bitmaps can quickly consume your available memory budget leading to an application crash due to the
dreaded exception OK.
Mobile devices typically have constrained system resources.
OK.
Android devices can have as little as 16 megabytes of memory available to a single application.
OK and this is very important.
OK and bitmaps as you can see here take up a lot of memory especially for rich images like photographs.
OK on here.
Let's actually go click on this.
Lot loading large bitmaps efficiently.
So I already opened that here in a new tab.
As you can see here it says images come in all shapes and sizes.
In many cases they are larger than required for a typical application user interface.
Ok for example the system Gallerie application displays photos taken using your Andray devices camera
which are typically much higher resolution than the screen density of your device.
OK and here it says Arete bitmap dimensions and type.
OK we can actually read the bitmap dimensions and type.
Both are here.
This is very important as you can see here.
I skate down version into memory.
Ok so here as you can see it says now that the image dimensions are known.
They can be used to decide if the full image should be loaded into memory or if a sub sampled version
should be loaded instead.
Here are some factors to consider.
Estimated memory usage of loading the full image in memory amount of memory you are willing to commit
to loading this image given any other memory requirements of your application.
Dimensions of the target image view or the UAE component that the image is to be loaded into.
So as you can see here the application or the dimensions of image view.
So let's actually find this clear case here.
As you can see is for example for the layout which I assign the value 100 and for the layout height
I again assign the value 100.
OK so these are the important factors that we need to consider.
For anyone to actually output some images to the screen and as you can see here it says for example
it's not warf loading our value pixel image.
As you can see here disvalue pics that image into memory if it will eventually be displayed in disvalue
pixel Formanek Farmdale in an image view.
OK to tell the decoder to subsample the image loading a smaller version into memory sets in sample size.
So this value is valuable.
We need to set this variable to true.
OK in your math factory that options object.
OK for example.
As you can see this is an example for this it bitmap OK but here in our application in a customized
Lisp view as you can see.
I created my file through the options.
OK so that they can actually call this valuable on this object.
OK.
Options start in sample size four.
OK.
And here I created an object of type bitmap.
OK.
Bitmapped factory that the resource.
OK.
So because of you actually have more images in inside of the project as you can see here.
We have these images inside of this adorable folder in our project.
So if we need we needed to actually call this D-CT resource here and on our factory.
OK.
So bitmapped factor that later D-CT resource.
So and this it actually accepts some arguments context that gets resources.
So here we need to actually refers to the resources of our application so context that gets resources.
And the second argument is the specific image state of our animal images.
Ari Okay and here as you can see we have another argument here options.
So these options of with scientists in sample size here as you can see disvalue for our image.
And here I am assigning this bitmap to to my IMJ animal image.
OK.
So let's actually run our project and see what happens.
So now as you can see of we have now this came down version of our images on these images views.
And as you can see the quality of our images are actually a little bit low.
OK low quality images.
And let's actually change this number four to number eight.
And the search you did on our project again.
So as you can see here again our application is running on this emulator and you can see that these
images are actually very low quality OK low quality images.
So let's actually change this.
And as you can see here although our images are actually a little bit low quality but they're not actually
taking too much memory so that our application actually doesn't crash.
As you can see here.
OK.
So if we for example change disvalue to number two are the quality of our images are going to be a little
bit high.
High quality images.
So now that's actually on our project again and here as you can see now we have high quality images.
Okay.
But I think this is not the right way to write our coats but here I just wanted to show you what is
this a bit more effectively that options.
OK how and how we can actually use the bitmap factory in order to actually see the images and output
those images to the screen.
OK.
So let's actually comment out these lines of code here.
OK.
And now according to the documentations we can actually use these if it's OK that the Android gives
us the Android documentation gives us as you can see we can't use this method so I just copied the Smurfette
here as you can see.
Now let's actually go to an android studio and after the end in curly brace of this method as you can
see of this get View method I want to paste this method here and then I'm going to explain to you what's
going on here.
So as you can see here this is our method.
OK.
And as you can see the Axis modifier of this and if it is public this method is static.
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
type of this if it is it and the name of this method is calculate in sample size.
OK.
So it's going I should calculate this impulse in sample size variable for us automatically according
to our images.
OK.
So this Biffen actually accepts some arguments between bitmapped fancy options options.
OK.
It requires.
It requires height.
OK.
And as you can see here inside of this it actually created some variables local variables.
The first one is constant int height as you can see and hear it put an assignment operator and then
it referred to this options argument that's outright.
So out how outside is going to actually give us the height of our image.
Okay.
And then here we have a V.
Okay.
And then it actually gives us the outwits for key the Viets of our image.
And then it's going to say that's why you do this.
It's constant.
OK.
And we have these int in sample size variable.
OK.
And it assigned a value in number one to this variable and it actually created an if statement here
as you can see if the value of this height as you can see here this local variable height this constant
is greater than the value of these required height that we have here.
Or if the value of these v as you can see here is greater than the value of these require Veach as you
can see here then it's going to actually execute these lines of code inside of these if statement.
OK.
So as you can see here it's actually created a comment for us.
It says calculate the largest sample size value that is a power of two and keeps both height and weight
larger request that height and if it's OK.