Upload Photo on Imageview Click Android Firebase

Upload Image to Firebase Storage | Android Tutorial

How to upload Image to Firebase Storage in Android

upload Image to Firebase Storage in Android

Hello world, today we are going to learn how we can upload an paradigm to the firebase storage from our android application. We will meet how nosotros canshow the progress of uploading an prototype to the user. Firebase storage gives utilize utility to upload the file and get the URL of that uploaded file so that we can access our file through the link. Let's encounter the terminal result we are going to learn.

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

Afterward creating the storage nosotros need to change our storage rule so that we can admission the storage with the hallmark.

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

After make changes hit the publish button and nosotros are fix to go.

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

Add together This in the project level build.gradle.

the first dependency is for firebase storage and the terminal dependency is for pick images from gallery or photographic camera.

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

  1. Selection an image from the gallery or photographic camera.
  2. Brandish picked image into ImageView.
  3. Upload epitome on Click of the push.
  4. Brandish progress bar while uploading.
  5. Afterward Uploading, become the uploaded image link.

Pick an image from the Gallery or Camera

The first step is to pick the image from the gallery or camera for that nosotros volition going to utilize this library https://github.com/Dhaval2404/ImagePicker. Let'south create our layout so that we can button and imageview. Open your activity_main.xml file and add together this code.

In the layout, nosotros accept an imageview to evidence the image when we choose from the gallery. A progress bar and a textview to show the progress of uploading an image and an upload push button to start uploading. A simple layout.

We will pick an prototype on the click of imageview nosotros demand to add click listener and write code for start action to selection an prototype. Come across the below lawmaking.

In the above lawmaking, we are starting the image picker and to become the image URI in the onActivityResult method. Permit'southward override this method in our grade.

Display picked image into ImageView

After getting the epitome URI nosotros volition set the URI to imageview so that the user can know which image he chose.

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

We are storing the prototype URI in the variable.

Encounter also: Android Bottom Canvass

Upload image on Click of the button

Now nosotros volition upload the image on the click of the push. Let's implement the on click role on the button.

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

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

And so we will add together a successful listener and failure listener. When your file successfully uploads then addOnSuccessListener called and when in that location will be any fault and then addOnFailureListener called.

Now encounter how to bear witness progress bar while uploading.

Display progress bar while uploading

To display progress while uploading the file or image. We will add addOnProgressListener to the UploadTask. Check the below code.

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

Then we set the progress of the progress bar and too gear up the text of textview to show the progress. This way we hands calculate the progress and gear up to the progress bar.

Sometimes we accept the case when we use putStrem and laissez passer InputeStrem object instead of putFile. In this case, if nosotros call the getTotalByteCount method we get -ane in return. To get the file size of InputStream due east can use bachelor method to get the electric current data size. Note that telephone call the bachelor method right after the InputStream method.

Now the only matter is left is to get the link of the file which we just uploaded.

Subsequently Uploading, get 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 manner we tin can go our download link of the file which we have just uploaded.

You tin can relieve this URL to the database for subsequently apply peradventure.

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

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

Thanks for reading have a nice mean solar day.

Subscribe to our Newsletter

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

hesterhavern.blogspot.com

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

0 Response to "Upload Photo on Imageview Click Android Firebase"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel