Android Upload Image to Firebase Full Size

Upload Image to Firebase Storage | Android Tutorial

How to upload Image to Firebase Storage in Android

upload Image to Firebase Storage in Android

How-do-you-do globe, today we are going to larn how we can upload an paradigm to the firebase storage from our android awarding. We volition see how we canshow the progress of uploading an image to the user. Firebase storage gives use utility to upload the file and get the URL of that uploaded file so that nosotros can admission our file through the link. Let's see the final result we are going to larn.

Earlier staring the lawmaking nosotros demand to enable firebase storage in our Firebase project. Open up your Firebase console and select the project and click on storage then follow the on-screen instructions.

After creating the storage we need to change our storage rule so that we can access the storage with the authentication.

Open your rules and change the if condition and supplant != to ==.

After make changes hit the publish push button and we are set up to go.

In your android project add together these dependencies to work with firebase storage.

Add This in the project level build.gradle.

the first dependency is for firebase storage and the last dependency is for option images from gallery or camera.

Later on adding the dependencies. Allow's see what tasks we demand to perform to upload an image to the firebase.

  1. Pick an prototype from the gallery or camera.
  2. Display picked prototype into ImageView.
  3. Upload image on Click of the button.
  4. Display progress bar while uploading.
  5. After Uploading, go the uploaded image link.

Choice an paradigm from the Gallery or Photographic camera

The commencement pace is to choice the image from the gallery or camera for that we will going to employ this library https://github.com/Dhaval2404/ImagePicker. Let's create our layout then that we tin can push button and imageview. Open your activity_main.xml file and add this code.

In the layout, we have an imageview to bear witness the image when we cull from the gallery. A progress bar and a textview to testify the progress of uploading an image and an upload button to start uploading. A unproblematic layout.

We will pick an image on the click of imageview nosotros need to add click listener and write lawmaking for start activity to selection an image. See the below code.

In the to a higher place lawmaking, we are starting the prototype picker and to get the image URI in the onActivityResult method. Let's override this method in our course.

Display picked image into ImageView

After getting the prototype URI we will set the URI to imageview and then that the user tin can know which image he chose.

Note: if you lot are getting FileNotFound Exception then add android:requestLegacyExternalStorage="true" to the application tag in the manifest.xml file.

We are storing the image URI in the variable.

Come across also: Android Bottom Canvas

Upload epitome on Click of the push

Now we will upload the image on the click of the button. Let's implement the on click function on the button.

First, we are getting the storage reference and so nosotros are uploading the image in the images folder and and then concatenate the original image proper noun with the file format using the URI method getLastPathSegment. This method returns the file name.

After that, we are uploading the image using the uploadImageRef and calling its method putFile and pass our file Uri.

Then nosotros volition add a successful listener and failure listener. When your file successfully uploads and so addOnSuccessListener called and when there will exist whatever mistake then addOnFailureListener called.

Now meet how to testify progress bar while uploading.

Display progress bar while uploading

To display progress while uploading the file or paradigm. Nosotros will add together addOnProgressListener to the UploadTask. Check the below code.

In the addOnProgressListener method, nosotros are calculating the progress by multiply 100 with data uploaded and divided by the total size of the file or epitome.

And then we set the progress of the progress bar and also prepare the text of textview to testify the progress. This way we hands summate the progress and ready to the progress bar.

Sometimes we have the example when we use putStrem and pass InputeStrem object instead of putFile. In this case, if we call the getTotalByteCount method nosotros go -i in return. To get the file size of InputStream e tin use bachelor method to go the current data size. Note that call the available method correct after the InputStream method.

Now the but thing is left is to become the link of the file which nosotros simply uploaded.

After Uploading, go the uploaded image link

To become the download link of the epitome or file we demand to add code in the success listener of the UploadTask.

This style nosotros tin get our download link of the file which we have just uploaded.

Yous can save this URL to the database for later on apply maybe.

Now you can run your project and your app will run smoothly.

If yous learned something new then do share this article with your friends and batchmates and don't forget to subscribe to our newsletter if y'all haven't joined already for the latest updates.

Thanks for reading have a overnice day.

Subscribe to our Newsletter

By subscribing to our newsletter yous're agreeing with our T&C.

yaganupped1982.blogspot.com

Source: https://www.akshayrana.in/2020/08/upload-image-to-firebase-storage-android.html

0 Response to "Android Upload Image to Firebase Full Size"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel