Hire everyone in this tutorial I want to show you how you can download and upload files to firebase.
So let's create a new Android studio project here.
So the application name is going to be upload and download files.
So here you can specify the company domain the partitioning and the project location.
So just going to connect here choose API 16 Android 4.1 jelly bean as the minimum SDK for phone and
tablet on next and here choose empty activity next.
And here don't change anything.
Just click on finish.
So now that a project is created let's actually connect this project to the Firebrace.
So at the top menu.
Just click on Tools and here click on Firebrace.
Let's wait a little bit here.
And now you can see that this assistent area is opened.
Click on storage and here click and upload and download a file with firebase storage.
So here first we need to connect to firebase.
Here you can see that these starts connect dialog opens.
So let's wait a little bit here and here it says create new firebase project.
So this is the name of the project.
And here you can specify the country region and click on Connect to firebase.
So now that the app is connected to firebase now it's time to add firebase storage to our application.
So just click on the spot in here at firebase threshed your app.
And here you can see these changes that will actually take place inside our project.
So just click and accept changes.
So let's wait a little bit here.
Here you can see it's cradle and it's actually doing something some processes are happening here.
So let's wait a little bit here.
So now it says dependences set up correctly.
So let's collapse this assistent area by just clicking on the spot in here.
So this is our project.
Now let's go to our activity Underland minute exam segment here.
And let's actually double click on the stop here in order to make this area of Vider selectees hello
world takes you here and and pre-press on Dorothy on your keyboard.
So here first of all we want to allow the user to upload an image to the actually Firebrace storage.
So here inside this pallet section just click on images here and you can see this image button image
view video view.
So for now we just we don't want to actually work with these objects.
Let's click on all and let's click on this button here select the spot and just track it here into this
constraint to lay out.
Notice that this is a constraint and not a relative or a linearly out.
So just put the button here.
I select spot button let's add the constraint to the top of this view.
So you can see this little circle here on the top edge of this button that is pulsating.
So let's create a constraint to the top of the view here.
So now here inside this property section you can see these constraints.
So for the top constraint Let's actually change this value to number 18 and let's select this button
here.
So for the ticks Let's actually just open here upload upload the image to firebase Firebrace and in
person until you are on your keyboard.
Let's give an idea to the Spartans select disputant.
So for the ideas they've been here PTEN upload image and that person entered January keyboard.
So now let's open our browser here now here inside the search.
Put up your web browser search for Picasso Android.
So we need to add this library to our project.
So here we can see this first link that appears here.
Picasso that square.
Open Source.
Let's click on it.
Let's wait a little bit here.
So now let's scroll down here.
So let's actually take a look at this introduction it says images add a much needed context and visual
flair to Android applications.
Picasa allows for hassle free image loading in your application.
Often in one line of code many common pitfalls of image loading up on Android are handled automatically
by Picasso handling image recycling and download cancellation in an adapter.
Complex image transformations with minimal memory use automatic memory and disk caching.
So let's scroll down here so let's see actually how we can add this library to our project solaces call
that.
And inside the download section.
Here we have the jar file and we have this cradle quote here.
So we need to add this code to our project just copy this line of code here.
Let's go back to android studio and let's actually open the Android view here.
So here you can see these great scripts open this may be that great fight the module app double click
on it and scroll down.
Here you can see this dependences block here right inside this dependences plug paste.
This line of code here that we actually copied from the Picasso website here and now you can see it
is now a button here that appears here is just click on it in order to see the project.
So let's wait and now we can see that the Picasso library is actually being added to our application.
But if you encounter any problems here at this point of your application make sure that you have updated
the Android repository packages of your Android SDK.
Make sure that you have updated to build tools and redeposited packages and Google Play services.
So we always make sure that you have updated all your packages that you have inside you as well that's
the case so in order to show you how we can update those packages.
If you have a Mac you just need to click on Android studio and then click on preferences.
If you have Windows you just need to click on file settings.
So I just need to click on preferences here.
Now here you can see inside is an appearance and behavior we have the system settings and we have this
Android SDK so you can see these actually.
These a hundred years ago for example there is an update for that.
And right now I get 7.1 point one 1 also.
The Android 8.0 is released but I don't recommend to actually just use it because at this point it may
be a little buggy so let's all wait until a more stable version of Android 8 is released and then use
it.
So for now we're going to use this version and we're seven hundred seven point 1.1 or Android 7.0 or
Android six point zero.
If you want to see the package details you can't just click on this package details here.
So now let's click on SDK tools now.
Make sure that you have installed and updated.
Andrew is the cable tools Android emulator and with STK platform tools Android our support library here
as you can see I have updated the Android support library to the latest version and this one Google
GGP with the blogging tools Google Play.
AP AP extension library Google Play licensing library Google Play services Google Google driver and
make sure that you actually have updated all these packages here and with support repository.
These ones.
So just click on OK now let's go back to Underwoods studio.
And here let's go to our main activity class.
So let me clean this up here in order to make this area wider.
So first of all let's just for example let's just declare a bottom here so just type in here private
BOTTEN PTEN upload image BT and upload image and then put a semicolon here.
Let's go to this uncreate Smurfette.
And here's just typing PTEN upload image assignment operator.
Let's cast this object to our pattern of text find view by id artist Id that PTEN upload image semicolon.
So now let's create a listener for this spot and let's make a spot and a listener to the changes that
is being occurred on the spot and so here I can see Betty and upload image that sets unclick listener.
Now you just hold the shift key on your keyboard and then presently they reach you on your keyboard
so that this option appears uncle at the center and you can see the curly braces and these free dots.
So just select this one and then press and and to your keyboard so the Android studio automatically
put these codes here for you so that this button is now a listener to the clicks or the touch of the
user.
So let's go inside is unclear method here.
Let's create a comment here.
So just step in here.
Upload the image to the server to this server.
Right.
So now let's actually create some references to the firebase instead.
So inside this main activity class let's create a variable of type Firebrace storage such as step in
here private Firebrace storage Firebrace storage and the name of this variable is Firebrace storage
semicolon.
Notice that you are not initializing these valuable here.
Right.
We are just declaring displayable here private and here let's create a variable of type storage reference
so that this variable is going to hold a reference to this for instance.
So so this reference and the name of this value bit is sort of a reference semicolon.
So let me first show you the web browser.
So here enter this you Arel firebase that Google that makes sure that you are assigning to your firebase
account.
And here click on go to console.
So here you can see the firebase protects here right.
So now let's click on our own project upload and download files so you can see that there is a typo
here.
So I forgot to put the P letter here.
So let's click on our project here.
Now you can see that this is our project right let's click on storage and here you can see these two
taps files and rules.
Right.
Let's click on rules.
So here we need to change the rule we need to say that allow read write if requested oath is equal to
null so that anyone can actually upload files to are actually storage here.
It's not really recommended to actually to change the sitting here.
It's not the command to say that everybody can upload files to our backend but for this application
it is OK but later you may want your users to be authenticated inside your application and only the
authenticated users can upload files to the backend.
So let's get we can publish now we can see it's as rules published.
Let's go back to the files tab.
So here you can see this you are right.
Look at look at this you are all here.
This is the you are ill that we can use to upload what we can actually.
We can also manually upload it to our patch and if I click on upload fight here.
So now for example I can select one of these images here for example Fox that is open right now it's
going to upload this image to our backend and later and we can download this image from our patch.
So for now we just want to learn how to upload files using Java codes in Android to Firefox.
So let's go back to android studio.
And here let's initialize our Firebrace storage instance.
So let's go inside these uncreate Smurfit here after this line of code that actually initializers this
PTEN upload image.
I can refer to this firebase storage here.
Assignment operator and here I can say Firebrace storage that gets instance semi-colon.
Right.
So now it's time to initialize our storage reference.
So just typing here storage to our friends storage reference assignment operator and refers to this
firebase smallest instance does get a reference from you or l.
So let's let me show you the web browser here.
So you can see this you are in here right.
If you click on this I can hear it says copy for you.
And let's click on it and it says copy to clipboard.
Let's go back to our Android studio and let's put two quotes here and paste this you are in here right
semicolon.
So now let me create a new line here so that you can see what's going on here.
So it's time to go inside.
It's unclear to me if of this Pythian upload image that's the thanklessly center here.
Now first we need to create an intent.
So just they've been here intent and the name of this internet is going to be image intent image intent
assignment operator new new intent and here inside the apprentice's.
I can't say intent that action Pietsch intent that action.
And then put a semicolon at the end of this line of code here.
So here just type in image intent data sets type and put two codes here and just type in here image
slash and at the end of this line of code just put a semicolon here.
So here we are specifying the type of the intent.
So we are going to access the images.
So the type is going to be Image slash.
And then here we need to call this method start activity for result.
First we need to pass the intense image intent comma.
So here we can specify the request code.
So for this specific application I'm going to pass that number one here if you want to we can create
a constant that holds this value number one and then each and past is constant here.
But for this application I just pass that value and number one here.
Let's scroll down here.
And before the end in curly brace of these main activity class.
So here we need to override the unactivated result method on activity result.
So this method is going to be executed when the user has selected an image from the gallery or anywhere
else and they use it is actually come back to the main activity.
Now the NITV to resolve if it is called.
So that's why here inside these Uncle Mefford we said start activity for results because we want to
get a result from that specific activity this image into it actually.
So inside this activity result I can create an if statement or I can actually create a switch statement
or switch switch request code.
Right and then open and close this switch statement by using the curly braces here.
So here we passed number one as the request to the Smif at right.
So here I can say.
Case number one colon.
So he had notice that we are referring to this request called argument of this unactivated is augmented.
Right.
Case Number one if there is that request code is actually number one.
I can create an if statement here so if result call it now I'm going to refer to this result called
arguments so if resolved code is equal to the result.
OK.
So now open and close this if statement by using these curly braces here.
So not just after this.
Notice that after this case statement make sure to actually put this break statement here.
Right.
So now inside these if statement we can't say so or not is that the Smurfit has this argument of type
intent and it is data.
Right.
So we get that data from the image intent.
So first we need to access the user of that specific image that is selected by the user.
So we can create a value of that you are here.
So you are right you are right.
And the name of this valuable is image.
You are the image you are the assignment operator.
Now refer to these arguments name data that get data.
So notice that this that gets the time if it actually returns a value of type you are.
I soon notice that these data type here determines or specifies the return type of the Smurfette which
is going to be you are right.
So just select the Smurfette and then press on and end up with a semicolon at the end of this line of
code here.
So now that we have this image you are right you can say.
So let's create a variable of types thought its reference in order to hold the reference of the images
that you're going to have inside our backend.
So solicitor or offense and the name of this variable is going to be Image address image address in
Firebrace assignment operator.
So now let's refer to our store as reference variable that we created above inside this class.
So just typing here.
So our friends thought so.
Because I want to actually create a folder inside our backend.
I'm going to create a chart.
So you're already familiar with these concepts because they are in their fiber section of course.
I talked a lot about how we can create a child how we can create a folder and how you can put your actually
files or data inside those forth.
So I'm going to create the child the name of this child is going to be images.
Right.
So now I want to put this image that the user has selected two into these images folder so I can say
again that child.
And now I'm going to refer to this image you are right so you can say image you or I did not get last
paff segment so this is going to be an address of that specific image that the user has selected.
If you want to you can create unique image names you know how to do that in the previous two tutorials.
I thought how you can create unique names so that you can actually use them inside your applications.
But for now I just want to use that last puff segment semi-colon.
So now let's create a new line here.
Let's create on line here and now after this line of code.
So in order to see that whether the image is actually uploaded to Firebrace or not.
We need to actually create an on a successor listener for these addressee or for this sort of reference
image address in firebase.
But before that we need to put the image into this address so we can say image address in firebase that
put file.
So not that this if it needs an argument of type you or I.
So we have this you are right.
Image You are right.
And here I can say that at on success.
Listener and just they've been here new and hold the shift key on your keyboard.
And the person then they're all on your keyboard and it says unsuccessfully send right.
Select this one and then put us on it which I knew.
So now the Android studio puts these lines of code here automatically for you.
So let me create a new line you know so that you can see the entire codes that are generated here.
Right.
These are the codes that are generated here.
E-mail address in firebase that putrified image you are right that ad unsuccessfully Center new unsuccess
listener upload task or task snapshots.
So the task snapshot is going to have the you are right that we need in order to actually download the
image later we are not going to use it now but we're going to actually use it later when we want to
download the image and put it into our image.
So this is the unsuccess method right.
The unsuccess method is going to be called then the uploading task is actually successful.
And when the image is successfully uploaded to their firebase.
But what happens if the image is not actually successfully uploaded to the firebase.
In that case we have actually an on failure it.
So let's put this in parenthesis here.
There is an error here it says put a semicolon here.
So don't put a semicolon here just put that here and select this one add on failure center and then
press and attention your keyboard and inside the apprentice's just open here.
You again hold the shift key on your keyboard and then press on the letter or key on your keyboard and
select this one on phileo listener and then present it to you on your keyboard so you can see that we
have this unfairly on my face.
So this is going to be executed when the uploading task was not successful and it actually failed.
So let's actually put a semicolon here at the end of this Prentice's here.
So now we have no errors right.
So let's go inside is unsuccess Smurfette.
Let's create a toast message toast that make takes the first minute to pass the context.
Don't just talk to typing this year because if you put this year here we actually created an anonymous
in a class that actually implements this unsuccessfully center interface.
So this here refers to that anonymous class not this main activity context so the context is actually
the main activity.
So just typing here main activity that this has the context come up put two quotes here.
So now we need to put the message here right.
And here I can say uploading was successful Let's put an exclamation mark here come up.
And here we just need to specify the duration of the message so they can hear and launch that show.
Right.
Let's copy this line of code and let's put it here inside this unfairly and method too.
And this time we can say uploading was not successful uploading was not successful and that's it.
So now it's time to run the application.
So let's run the application and let's see what happens now.
So just select one of these devices and then click on OK.
So now you can see that I opened the other browser here and I want to show you the emulator here.
So let's click on this button upload the image to Firebrace.
So it's going to actually open the gallery for us so I have actually taken these images if you want
to.
You can actually to use the camera of the emulator here and then you can actually just take some pictures
here.
Let's go back to our app here.
Upload and download files.
So select one of these images and now you can see that our application crashed.
There is a problem here.
Let's go back to android studio now.
If you want to find the error.
I just need to click on this android monitor.
So let me show you the error here and here look at these lines of code here.
These these are the texts actually.
And it says Jobar that line that security except exception permission denied.
So there is a permission error here.
And if I show you it says that we need to have read external storage permission this is actually a dangerous
permission and we need to actually declare this permission.
In both of our files the manifest file and the job of life.
So let's go to our Android studio and here let's open the ad with you.
Let's just double click on this manifests here.
So let me stop here in order to make this area right there.
So let me just put the spin machine here inside this manifest.
So just open it up and just type in here use this permission and just type in here right.
External storage right.
External storage and just disclosed is that if you choose a right external storage your application
is going to have also permission to read the external storage.
Your application is going to have to the permission to read and write the external storage.
So let's go to automate an activity class let's scroll to the top here and now here.
After declaring Desplat in here we need to actually say that you're going to have our permission.
We are going to ask the user to give us the permission to access their external storage.
So just create a variable of type integer.
Here inside is uncreate Mefford.
And then the name of this variable is going to be permission result.
Assignment operator and just type in here context compact that chick's self permission.
So for the first argument we need to pass the context so the context is main activity that this come
on.
And the second argument is going to be just type in here manifest.
So choose this one here and manifest Prentice's Android.
OK.
That permission that's right.
External sort of streite external storage.
So this is our permission write permission resolved.
So let me create a new line here.
So now that we have this permission result let's actually create an if statement so if permission result
is equal to package manager that permission granted.
This means that if the user has actually given us the permission to access the external storage then
we can actually put some lines of code here but we can actually we need to create an else statement
here in case the user doesn't give us actually permission to access that external storage.
So in that case we can actually say.