Firebase Storage Example – Uploading and Retrieving Files

In a previous tutorial we learnt about Firebase Storage. But I didn’t cover retrieval of the uploaded files from Firebase Storage. So here is another Firebase Storage Example. In this tutorial we will learn Uploading Files to Firebase Storage and Retrieving Uploaded Files from Firebase Storage. So lets start this Firebase Storage Example.

Creating Layout

Now we need to build the following layout for activity_main.xml.

activity_main.xml

We have the following components in the above layout.Choose Button: We will tap this button to choose an image from the gallery.EditText Enter Name: In this EditText we will put the label for the chosen image.ImageView: The middle area (you cann’t see it in the layout because it is blank ImageView) contains an ImageView where we will display the chosen image.Upload Button: Tapping this button will upload the selected image to Firebase Storage.TextView Uploads: Tapping this TextView will open another activity where we will display the uploaded images with labels.

Building Database Model

First we will create a class to store the Image data to Firebase Database. So create a class named Upload.java.

Upload.java

Java

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

packagenet.simplifiedcoding.firebaseupload;

importcom.google.firebase.database.IgnoreExtraProperties;

/**

* Created by Belal on 2/23/2017.

*/

@IgnoreExtraProperties

publicclassUpload{

publicStringname;

publicStringurl;

// Default constructor required for calls to

// DataSnapshot.getValue(User.class)

publicUpload(){

}

publicUpload(Stringname,Stringurl){

this.name=name;

this.url=url;

}

publicStringgetName(){

returnname;

}

publicStringgetUrl(){

returnurl;

}

}

Getting Selected File Extension

We will rename the file to a unique name as if we upload the file with same name then files would be overwritten. So after renaming the file the extension should remain the same. So inside MainActivity.java create a method getFileExtension() and it will return as the extension of the selected file by taking Uri object.

Java

1

2

3

4

5

publicStringgetFileExtension(Uri uri){

ContentResolver cR=getContentResolver();

MimeTypeMap mime=MimeTypeMap.getSingleton();

returnmime.getExtensionFromMimeType(cR.getType(uri));

}

Uploading the File to Firebase Storage

Now we will create a method inside MainActivity.java that will upload the selected file to Firebase Storage. So create a method name uploadFile() and write the following code.

So thats it for Firebase Storage Example. If you are facing problems then let me know in the comment section and I will try to help you out. Also don’t forget to share this Firebase Storage Example if you found it useful. Thank You 🙂

bro go to storage section. then select rules tab. there you will see instructions to enable API and a link for this purpose will be given in these instructions. Go and just follow link.and after this, again go to rule tab of storage just make little amendment i.e. replace != with ==

it show “This method should only be accessed from tests or within private scope less… (Ctrl+F1)
This inspection looks at Android API calls that have been annotated with various support annotations (such as RequiresPermission or UiThread) and flags any calls that are not using the API correctly as specified by the annotations. Examples of errors flagged by this inspection:
Passing the wrong type of resource integer (such as R.string) to an API that expects a different type (such as R.dimen).
Forgetting to invoke the overridden method (via super) in methods that require it
Calling a method that requires a permission without having declared that permission in the manifest
Passing a resource color reference to a method which expects an RGB integer value.
…and many more”

Belal, i need help in a code, I have several data in the firebase database, a latitude and a longitude in the children, I want to retrieve all this data and add bookmarks in Google Maps in the app. I made this code below and it is giving error. What did I do wrong?

Hi, thanks for tutorial it works great :)) Can I make the same app with videos ? I change intent.setType but it’s only affects uploading as I understand. How can I retrieve videos and see them in recyclerview??

Thank you for the tutorial everything works fine. if i manually upload a file using the firebase console and then click on view to see the images on the firebase it only shows images that i have uploaded from my own phone. not the one present on the firebase storage.

Thank you Brother, I like your kotlin android videos. Just now I learned firebase kotlin from your video. I am requesting you to make a firestore kotlin video from step by step. I am getting any video for that in the internet. If I got also they are not explaining from step by step like you.

Privacy & Cookies: This site uses cookies. By continuing to use this website, you agree to their use.
To find out more, including how to control cookies, see here:
Cookie Policy

About Simplified Coding

Simplified Coding is a blog for all the students learning programming. We are providing various tutorials related to programming and application development. You can get various nice and simplified tutorials related to programming, app development, graphics designing and animation. We are trying to make these things simplified and entertaining. We are writing text tutorial and creating video and visual tutorials as well. You can check about the admin of the blog here and check out our sitemap