Simplified Coding

  • About
  • Contact Us
  • Advertise
  • Privacy Policy
You are here: Home / Android Application Development / Android Advance / Firebase Storage Example – Uploading and Retrieving Files

Firebase Storage Example – Uploading and Retrieving Files

February 24, 2017 by Belal Khan 65 Comments

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.

If you are dealing with PDF Files Follow this Tutorial
Firebase Storage Upload and Download PDF Files

Contents

  • 1 Firebase Storage Example Video Demo
  • 2 Firebase Storage Example
    • 2.1 Creating a New Project
    • 2.2 Setting Up Firebase Storage and Firebase Database
    • 2.3 Creating Layout
    • 2.4 Defining Firebase Constants
    • 2.5 Defining View Objects and Constants
    • 2.6 Adding File Chooser
    • 2.7 Uploading File to Firebase Storage and Saving URL to Firebase Database
      • 2.7.1 Building Database Model
      • 2.7.2 Getting Selected File Extension
    • 2.8 Uploading the File to Firebase Storage
    • 2.9 Retrieving Files from Firebase Storage
      • 2.9.1 Adding RecyclerView and CardView
      • 2.9.2 Adding Glide
      • 2.9.3 Creating RecyclerView Layout
      • 2.9.4 Creating RecyclerView Adapter
    • 2.10 Sharing is Caring:
    • 2.11 Related

Firebase Storage Example Video Demo

  • You can watch this video to know what we are going to build in this tutorial.

Firebase Storage Example

  • As always the first step is creating a new Android Studio Project.

Creating a New Project

  • I have just created a project named FirebaseUpload.
  • In the project we will be uploading images to Firebase Storage and then we will fetch back the uploaded images to display them into RecyclerView.
  • So now we will setup firebase storage to our project.

Setting Up Firebase Storage and Firebase Database

  • We will upload the images to Firebase Storage and in the Firebase Database we will store the image URL. For this we need to setup both in our project.
  • Go to tools -> firebase

setting firebase

  • Now you will see an Assistant Window, you can setup Firebase Database from here.
firebase database

Firebase Database

  • The same way you can setup Firebase Storage.

firebase storage

  • If you are confused about this step go through the last Firebase Storage Tutorial where I explained it in detail.

Creating Layout

  • Now we need to build the following layout for activity_main.xml.
firebase storage example

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.
  • You can use the following xml code to build the above layout.

activity_main.xml
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="net.simplifiedcoding.firebaseupload.MainActivity">
 
    <LinearLayout
        android:id="@+id/linearLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
 
        <Button
            android:id="@+id/buttonChoose"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Choose" />
 
        <EditText
            android:id="@+id/editText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="4"
            android:hint="Enter name for Image" />
 
    </LinearLayout>
 
 
    <ImageView
        android:id="@+id/imageView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_below="@+id/linearLayout" />
 
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:orientation="vertical">
 
        <Button
            android:id="@+id/buttonUpload"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Upload" />
 
        <TextView
            android:id="@+id/textViewShow"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="10dp"
            android:text="View Uploads"
            android:textAlignment="center"
            android:textColor="@color/colorPrimary"
            android:textStyle="bold" />
    </LinearLayout>
</RelativeLayout>

Defining Firebase Constants

  • Create a java class named Constants.java and define the following.

Constants.java
Java
1
2
3
4
5
6
7
8
9
10
11
package net.simplifiedcoding.firebaseupload;
 
/**
* Created by Belal on 2/23/2017.
*/
 
public class Constants {
 
    public static final String STORAGE_PATH_UPLOADS = "uploads/";
    public static final String DATABASE_PATH_UPLOADS = "uploads";
}

Defining View Objects and Constants

  • Come inside MainActivity.java and define the following objects and constants.

MainActivity.java
Java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    
    //constant to track image chooser intent
    private static final int PICK_IMAGE_REQUEST = 234;
 
    //view objects
    private Button buttonChoose;
    private Button buttonUpload;
    private EditText editTextName;
    private TextView textViewShow;
    private ImageView imageView;
 
    //uri to store file
    private Uri filePath;
 
    //firebase objects
    private StorageReference storageReference;
    private DatabaseReference mDatabase;

  • Now inside onCreate() we will initialize the the objects and attach click listeners.

Java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        buttonChoose = (Button) findViewById(R.id.buttonChoose);
        buttonUpload = (Button) findViewById(R.id.buttonUpload);
        imageView = (ImageView) findViewById(R.id.imageView);
        editTextName = (EditText) findViewById(R.id.editText);
        textViewShow = (TextView) findViewById(R.id.textViewShow);
 
        storageReference = FirebaseStorage.getInstance().getReference();
        mDatabase = FirebaseDatabase.getInstance().getReference(Constants.DATABASE_PATH_UPLOADS);
 
        buttonChoose.setOnClickListener(this);
        buttonUpload.setOnClickListener(this);
        textViewShow.setOnClickListener(this);
    }

Adding File Chooser

  • Define a new method showFileChooser().

Java
1
2
3
4
5
6
    private void showFileChooser() {
        Intent intent = new Intent();
        intent.setType("image/*");
        intent.setAction(Intent.ACTION_GET_CONTENT);
        startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_REQUEST);
    }

  • Now we need to handle the Intent result. For this we will override the method onActivityResult().

1
2
3
4
5
6
7
8
9
10
11
12
13
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) {
            filePath = data.getData();
            try {
                Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), filePath);
                imageView.setImageBitmap(bitmap);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

  • Now we will call the method showFileChooser() when the choose image button is clicked. For this come inside the overriden method onClick() and write the following code.

1
2
3
4
5
6
7
8
9
10
    @Override
    public void onClick(View view) {
        if (view == buttonChoose) {
            showFileChooser();
        } else if (view == buttonUpload) {
 
        } else if (view == textViewShow) {
 
        }
    }

  • Now try running the application and you will see that choose image is working fine.

choose image

  • Now we will code the upload functionality.

Uploading File to Firebase Storage and Saving URL to Firebase Database

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
package net.simplifiedcoding.firebaseupload;
 
import com.google.firebase.database.IgnoreExtraProperties;
 
/**
* Created by Belal on 2/23/2017.
*/
@IgnoreExtraProperties
public class Upload{
 
    public String name;
    public String url;
 
    // Default constructor required for calls to
    // DataSnapshot.getValue(User.class)
    public Upload() {
    }
 
    public Upload(String name, String url) {
        this.name = name;
        this.url= url;
    }
 
    public String getName() {
        return name;
    }
 
    public String getUrl() {
        return url;
    }
}

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
    public String getFileExtension(Uri uri) {
        ContentResolver cR = getContentResolver();
        MimeTypeMap mime = MimeTypeMap.getSingleton();
        return mime.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.

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
    private void uploadFile() {
        //checking if file is available
        if (filePath != null) {
            //displaying progress dialog while image is uploading
            final ProgressDialog progressDialog = new ProgressDialog(this);
            progressDialog.setTitle("Uploading");
            progressDialog.show();
 
            //getting the storage reference
            StorageReference sRef = storageReference.child(Constants.STORAGE_PATH_UPLOADS + System.currentTimeMillis() + "." + getFileExtension(filePath));
 
            //adding the file to reference
            sRef.putFile(filePath)
                    .addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                        @Override
                        public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                            //dismissing the progress dialog
                            progressDialog.dismiss();
                            
                            //displaying success toast
                            Toast.makeText(getApplicationContext(), "File Uploaded ", Toast.LENGTH_LONG).show();
                            
                            //creating the upload object to store uploaded image details
                            Upload upload = new Upload(editTextName.getText().toString().trim(), taskSnapshot.getDownloadUrl().toString());
                            
                            //adding an upload to firebase database
                            String uploadId = mDatabase.push().getKey();
                            mDatabase.child(uploadId).setValue(upload);
                        }
                    })
                    .addOnFailureListener(new OnFailureListener() {
                        @Override
                        public void onFailure(@NonNull Exception exception) {
                            progressDialog.dismiss();
                            Toast.makeText(getApplicationContext(), exception.getMessage(), Toast.LENGTH_LONG).show();
                        }
                    })
                    .addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() {
                        @Override
                        public void onProgress(UploadTask.TaskSnapshot taskSnapshot) {
                            //displaying the upload progress
                            double progress = (100.0 * taskSnapshot.getBytesTransferred()) / taskSnapshot.getTotalByteCount();
                            progressDialog.setMessage("Uploaded " + ((int) progress) + "%...");
                        }
                    });
        } else {
            //display an error if no file is selected
        }
    }

  • Now we need to call this method on Upload Button Click. So modify onClick() method as below.

Java
1
2
3
4
5
6
7
8
9
10
    @Override
    public void onClick(View view) {
        if (view == buttonChoose) {
            showFileChooser();
        } else if (view == buttonUpload) {
            uploadFile();
        } else if (view == textViewShow) {
 
        }
    }

  • Now try running your application and upload will work.

firebase storage upload

Retrieving Files from Firebase Storage

  • Now we will create a new activity where we display all the uploaded images with labels.
  • So create a new activity named ShowImagesActivity and inside the layout file of this activity we will create a RecyclerView.

Adding RecyclerView and CardView

  • First we need to add dependencies for RecyclerView and CardView.
  • Go to File -> Project Structure and go to dependencies tab. Here click on plus icon and select Library dependency.

adding recyclerview

  • Now here you need to find and add RecyclerView and CardView.

adding recyclerview

  • Now sync your project.

Adding Glide

  • We also need to add Glide Library. As we need to fetch images from the URL and for this I will be using Glide Library.
  • So just modify  dependencies block of your app level build.gradle file as below.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:25.2.0'
    compile 'com.google.firebase:firebase-storage:10.2.0'
    compile 'com.google.firebase:firebase-auth:10.2.0'
    compile 'com.google.firebase:firebase-database:10.2.0'
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:recyclerview-v7:25.2.0'
    compile 'com.android.support:cardview-v7:25.2.0'
 
    //adding glid library
    compile 'com.github.bumptech.glide:glide:3.7.0'
}

  • Now sync the project with gradle.

Creating RecyclerView Layout

  • Now we will design the layout for our RecyclerView. It will contain an ImageView and a TextView inside a CardView. So create a new layout resource file named layout_images.xml.

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
32
33
34
35
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">
 
    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="8dp"
        android:padding="8dp">
 
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:minHeight="100dp">
 
 
            <TextView
                android:id="@+id/textViewName"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Name"
                android:textAlignment="center" />
 
            <ImageView
                android:id="@+id/imageView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@id/textViewName" />
 
        </RelativeLayout>
 
    </android.support.v7.widget.CardView>
</LinearLayout>

  • So our layout for RecyclerView is ready. Now we will create an Adapter.

Creating RecyclerView Adapter

  • Create a class named MyAdapter.java and write the following code.

MyAdapter.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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package net.simplifiedcoding.firebaseupload;
 
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
 
import com.bumptech.glide.Glide;
 
import java.util.List;
 
/**
* Created by Belal on 2/23/2017.
*/
 
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
 
    private Context context;
    private List<Upload> uploads;
 
    public MyAdapter(Context context, List<Upload> uploads) {
        this.uploads = uploads;
        this.context = context;
    }
 
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.layout_images, parent, false);
        ViewHolder viewHolder = new ViewHolder(v);
        return viewHolder;
    }
 
    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        Upload upload = uploads.get(position);
 
        holder.textViewName.setText(upload.getName());
 
        Glide.with(context).load(upload.getUrl()).into(holder.imageView);
    }
 
    @Override
    public int getItemCount() {
        return uploads.size();
    }
 
    class ViewHolder extends RecyclerView.ViewHolder {
 
        public TextView textViewName;
        public ImageView imageView;
 
        public ViewHolder(View itemView) {
            super(itemView);
 
            textViewName = (TextView) itemView.findViewById(R.id.textViewName);
            imageView = (ImageView) itemView.findViewById(R.id.imageView);
        }
    }
}

  • Now come inside layout file of this ShowImagesActivity and add a RecyclerView.

activity_show_images.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_show_images"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="net.simplifiedcoding.firebaseupload.ShowImagesActivity">
 
    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
 
    </android.support.v7.widget.RecyclerView>
 
</RelativeLayout>

  • Now write the following code inside ShowImagesActivity.java

ShowImagesActivity
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package net.simplifiedcoding.firebaseupload;
 
import android.app.ProgressDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.widget.Toast;
 
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
 
import java.util.ArrayList;
import java.util.List;
 
public class ShowImagesActivity extends AppCompatActivity {
    //recyclerview object
    private RecyclerView recyclerView;
 
    //adapter object
    private RecyclerView.Adapter adapter;
 
    //database reference
    private DatabaseReference mDatabase;
 
    //progress dialog
    private ProgressDialog progressDialog;
 
    //list to hold all the uploaded images
    private List<Upload> uploads;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_show_images);
 
 
        recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
 
 
        progressDialog = new ProgressDialog(this);
 
        uploads = new ArrayList<>();
 
        //displaying progress dialog while fetching images
        progressDialog.setMessage("Please wait...");
        progressDialog.show();
        mDatabase = FirebaseDatabase.getInstance().getReference(Constants.DATABASE_PATH_UPLOADS);
 
        //adding an event listener to fetch values
        mDatabase.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot snapshot) {
                //dismissing the progress dialog
                progressDialog.dismiss();
 
                //iterating through all the values in database
                for (DataSnapshot postSnapshot : snapshot.getChildren()) {
                    Upload upload = postSnapshot.getValue(Upload.class);
                    uploads.add(upload);
                }
                //creating adapter
                adapter = new MyAdapter(getApplicationContext(), uploads);
 
                //adding adapter to recyclerview
                recyclerView.setAdapter(adapter);
            }
 
            @Override
            public void onCancelled(DatabaseError databaseError) {
                progressDialog.dismiss();
            }
        });
 
    }
}

  • Thats it now just run your application and you will see all the fetched images in the next activity.

firebase storage example

  • Bingo! Its working absolutely fine.  Now if you are having troubles or confusions you can get my source code from the link given below.

Firebase Storage Example Source Code Download

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 🙂

Sharing is Caring:

  • Tweet
  • Share on Tumblr
  • More
  • Pocket
  • Print
  • Email

Related

Filed Under: Android Advance, Android Application Development Tagged With: firebase, firebase storage, firebase storage example

About Belal Khan

I am Belal Khan, I am currently pursuing my MCA. In this blog I write tutorials and articles related to coding, app development, android etc.

Comments

  1. Anonymous says

    February 25, 2017 at 3:15 am

    tq belal khan you are the best.. but how can i use this for pdf file? any idea? your help will be appreciated..

    Reply
    • Belal Khan says

      February 25, 2017 at 3:22 am

      In file chooser method instead of this
      intent.setType(“image/*”);

      use this
      intent.setType(“application/pdf”);

      Reply
      • vicky says

        June 4, 2018 at 2:46 pm

        Mr. Belal Reply for this:
        i got error on Tasksnapshot , taskSnapshot.getDownloadUrl().toString());

        i give permission to write external storage too…

        Reply
        • Swapnil Kant says

          June 14, 2018 at 5:10 pm

          i am also getting the same error please help

          Reply
          • Abhiraman Kuntimaddi says

            August 11, 2018 at 8:39 am

            getDownloadUrl() is deprecated.

            In order to use that you have to write or add the following

            taskSnapshot.getMetadata().getReference().getDownloadUrl().toString());

        • CodeX says

          October 4, 2018 at 2:23 pm

          You need to change your Firebase database,storage & authentication dependencies version. I am sure it is 10.0.1 like. To slove this error change dependencies 11.0.4 like version

          Reply
  2. Aaditya says

    March 7, 2017 at 7:11 pm

    The Recycler view is empty and does not show the uploaded images. Please help

    Reply
    • Aaditya says

      March 8, 2017 at 9:13 am

      Hello. Thanks a lot for this tutorial. How can i display the username of the person uploading the image.

      Reply
    • Rp says

      March 10, 2017 at 1:23 pm

      the Recycler view is empty and does not show the uploaded images. Please help me 2.

      Reply
      • Aaditya says

        March 10, 2017 at 6:12 pm

        Add compile ‘com.android.support:recyclerview-v7:25.2.0’ to your app gradle

        Reply
        • Jonh says

          March 11, 2017 at 12:00 am

          I get an error when I add that, is version 25 necessary?

          Reply
          • Aaditya says

            March 11, 2017 at 6:02 am

            No. Just add the recycler view in the app gradle

        • John says

          March 15, 2017 at 1:23 am

          I added it, still says RecyclerView is empty

          Reply
      • Mintu rabha says

        May 3, 2017 at 7:19 am

        check you might haven’t add cardview dependency…

        Reply
    • Soundarya says

      March 25, 2017 at 11:41 am

      Please check whether you have added the rules for Firebase database. Like below

      {
      “rules”: {
      “.read”: true,
      “.write”: true
      }
      }

      Also check permission in manifest,

      Reply
      • Zakaria Mansoor says

        June 15, 2017 at 2:00 pm

        this solved by prob, thanx (Y)

        Reply
      • sagar says

        September 3, 2018 at 7:08 pm

        what about the manifest permissions can you explain please

        Reply
  3. Ali says

    March 14, 2017 at 1:41 pm

    Hello,
    You didn’t handle READ_EXTERNAL_STORAGE, this permission is considered a dangerous permission and runtime permission should be handled,
    Am i missing something here?

    Thanks

    Reply
    • KIVANÇ says

      March 29, 2017 at 4:16 pm

      This could help;
      Under your class line
      -public static final int REQUEST_ID_MULTIPLE_PERMISSIONS = 2017;
      and check this method in “onCreate”
      -if (!checkAndRequestPermissions()) {
      return;
      }
      here is multiple request permission method,
      private boolean checkAndRequestPermissions() {
      int permissionINTERNET = ContextCompat.checkSelfPermission(this, android.Manifest.permission.INTERNET);
      int permissionACCESS_NETWORK_STATE = ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_NETWORK_STATE);
      int permissionREAD_EXTERNAL_STORAGE = ContextCompat.checkSelfPermission(this, android.Manifest.permission.READ_EXTERNAL_STORAGE);
      int permissionWRITE_EXTERNAL_STORAGE = ContextCompat.checkSelfPermission(this, android.Manifest.permission.WRITE_EXTERNAL_STORAGE);
      List listPermissionsNeeded = new ArrayList();
      if (permissionINTERNET != PackageManager.PERMISSION_GRANTED) {
      listPermissionsNeeded.add(android.Manifest.permission.INTERNET);
      }
      if (permissionACCESS_NETWORK_STATE != PackageManager.PERMISSION_GRANTED) {
      listPermissionsNeeded.add(android.Manifest.permission.ACCESS_NETWORK_STATE);
      }
      if (permissionREAD_EXTERNAL_STORAGE != PackageManager.PERMISSION_GRANTED) {
      listPermissionsNeeded.add(android.Manifest.permission.READ_EXTERNAL_STORAGE);
      }
      if (permissionWRITE_EXTERNAL_STORAGE != PackageManager.PERMISSION_GRANTED) {
      listPermissionsNeeded.add(android.Manifest.permission.WRITE_EXTERNAL_STORAGE);
      }
      if (!listPermissionsNeeded.isEmpty()) {
      ActivityCompat.requestPermissions(this, listPermissionsNeeded.toArray(new String[listPermissionsNeeded.size()]), REQUEST_ID_MULTIPLE_PERMISSIONS);
      return false;
      }
      return true;
      }
      hope this help. 🙂

      Reply
    • Hafiz says

      September 24, 2017 at 3:44 am

      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 ==

      Reply
  4. Vishalkumar says

    March 15, 2017 at 4:26 pm

    Can we view the uploaded file offline after downloading

    Reply
  5. Manivanna perumal says

    March 17, 2017 at 7:49 am

    i got error on Tasksnapshot , taskSnapshot.getDownloadUrl().toString());

    i give permission to write external storage too…

    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”

    Reply
    • KIVANÇ says

      March 29, 2017 at 4:11 pm

      You should check this link.
      http://stackoverflow.com/questions/41105586/android-firebase-tasksnapshot-method-should-only-be-accessed-within-privat

      Reply
  6. angel says

    March 20, 2017 at 9:00 am

    The images are not displaying when i click view upload button. i have already include all the libraries. Still it is not working. please help

    Reply
  7. Kıvanç says

    March 29, 2017 at 3:45 pm

    Thanks Belal, works perfectly.

    Reply
    • KIVANÇ says

      March 30, 2017 at 10:59 pm

      Sorry for bothering, I tried to make delete function like this:
      private boolean deleteImage(String id) {
      DatabaseReference mDatabase = FirebaseDatabase.getInstance().getReference(Constants.DATABASE_PATH_UPLOADS).child(id);
      // Delete the file
      mDatabase.removeValue();
      // announcement that image deleted.
      Toast.makeText(getApplicationContext(), “Image Deleted”, Toast.LENGTH_LONG).show();
      return true;
      }
      But not succeed, how can I do this?

      Reply
  8. shalika says

    April 3, 2017 at 12:41 pm

    i upload pdf file . how to show pdf file image recycleview

    Reply
  9. Sadeny says

    April 8, 2017 at 9:58 am

    Hello,
    This is a nice tutorial. I got this error while adding the realtime database to my app
    Error:(28, 13) Failed to resolve: com.google.firebase:firebase-auth:9.2.1
    you can help me

    Reply
    • Mohan says

      May 4, 2017 at 10:54 am

      Add the dependencies of firesbase in app gradle.

      Reply
  10. radhey says

    April 12, 2017 at 12:20 pm

    how to retrieve data in gridview

    Reply
  11. Suha says

    April 18, 2017 at 5:32 pm

    How to delete an image in this tutorial?

    Reply
  12. Ng R says

    April 24, 2017 at 10:47 pm

    I got an error

    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.ri.cesic/com.example.ri.cesic.UploadActivity}: java.lang.NullPointerException: Attempt to invoke virtual method ‘void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)’ on a null object reference

    What is wrong?

    Reply
  13. Mintu rabha says

    May 3, 2017 at 7:27 am

    can i view the uploaded files in different application….

    Reply
  14. Mohan says

    May 4, 2017 at 10:52 am

    when I click upload button it shows me the users does not have permission to access this project. can anyone tell me to resolve this.

    Reply
    • Anurag says

      June 25, 2017 at 6:44 pm

      Add these permissions, should resolve the issue:

      Reply
  15. Ali says

    June 11, 2017 at 11:25 am

    which Method call on textviewshow Button ..

    Reply
  16. Anurag says

    June 25, 2017 at 6:38 pm

    Hello,

    In the last activity (ShowImagesActivity.java), in the line:

    mDatabase = FirebaseDatabase.getInstance().getReference(Constants.DATABASE_PATH_UPLOADS);

    the parameter –> Constants.DATABASE_PATH_UPLOADS is throwing error. Please help. All the dependencies and the files are added properly and manifest is also updated.

    Reply
    • Anurag says

      June 25, 2017 at 6:41 pm

      The error is :

      “Cannot resolve symbol ‘Constants’ “

      Reply
  17. Anurag says

    June 25, 2017 at 10:16 pm

    I am getting the folowing error:

    java.lang.NullPointerException: Attempt to invoke interface method ‘int java.util.List.size()’ on a null object reference
    at com.example.anurag_g01.secretspace.UploadImageAdapter.getItemCount(UploadImageAdapter.java:50)

    Reply
    • Anurag says

      June 25, 2017 at 10:18 pm

      When ShowImagesActivity is called, then I am getting the above error

      Reply
  18. kalyan says

    June 30, 2017 at 1:03 pm

    how to display the scanned code in imageview using firebase

    Reply
  19. Francisco says

    August 2, 2017 at 10:35 pm

    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?

    public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;
    DatabaseReference ref = FirebaseDatabase.getInstance().getReference();

    ref.addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
    if (dataSnapshot.exists()) {
    double latitude = dataSnapshot.child(“latitude”).getValue(Double.class);
    double longitude = dataSnapshot.child(“longitude”).getValue(Double.class);
    LatLng local = new LatLng(latitude, longitude);
    mMap.addMarker(new MarkerOptions().position(local).title(“New Marker”));
    mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(local,18));
    } else {
    Log.e(TAG, “onDataChange: No data”);
    }

    }
    @Override
    public void onCancelled(DatabaseError databaseError) {
    throw databaseError.toException();
    }
    });

    Reply
  20. Dhan says

    August 20, 2017 at 6:35 pm

    Images are not displayed in recyclerView please help me

    Reply
  21. Suyog says

    August 30, 2017 at 6:32 am

    its give me filename= null due to this it unable to upload image in this

    Reply
  22. Ozge says

    September 9, 2017 at 3:27 pm

    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??

    Reply
  23. poeja says

    September 29, 2017 at 11:25 am

    hai, this is great tutorial
    i want to ask a question. How to sort it with the lastest upload?

    i mean, in the view uploads, how can we show the newest uploaded images on the top? when i try to upload a new images it will be on the bottom of the list.

    thanks for that

    Reply
  24. Mohammed Nashit says

    October 10, 2017 at 7:21 am

    Thank you belal bhai… You are the best…. was trying this from past 1 week…. your code did the work in 1 hr 😛

    Reply
  25. Karthikeyan says

    October 10, 2017 at 9:57 pm

    HI, how to compress and upload photo file to firebase storage..

    Reply
  26. najeeb says

    October 15, 2017 at 3:06 am

    i want o display firebase images in deatail activity on click ……………………………………..!!!

    Reply
  27. zaid says

    October 16, 2017 at 10:39 am

    how i appply click events on this images?

    Reply
  28. Rahul Roushan says

    October 30, 2017 at 1:36 pm

    when i click on view uploads app gets crashed please help me

    Reply
  29. Ferdi says

    November 3, 2017 at 8:59 pm

    can someone help me? image not displaying, i do everything on the tutorial

    Reply
  30. Divya says

    November 20, 2017 at 6:00 am

    With URL we can load images into Imageview but How can we save the images and audio uploads in firebase to our sdcard

    Reply
  31. Steve says

    November 26, 2017 at 8:03 am

    Thank you Belal Khan, btw can i use this method to upload text file as well?

    Reply
  32. vinayak says

    January 5, 2018 at 4:56 am

    i click choose button and selecting an image from gallery but the image is not displayed in image view.and when i click upload it shows unknown error occurred

    Reply
  33. vijay says

    January 10, 2018 at 2:24 pm

    Hi
    I am vijay, from this tutorial when i show image and text on another activity then it show “permission denied”.
    please help me.

    Reply
  34. raja says

    February 17, 2018 at 5:14 pm

    how to see uploaded photos through by viewupload(textview)..please help me.

    Reply
  35. mahad says

    June 14, 2018 at 6:18 pm

    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.

    Reply
  36. bhumi says

    July 13, 2018 at 12:45 pm

    I’m not getting image in my recyclerview using this tutorial

    Reply
  37. Zeeshan Rehmat says

    July 17, 2018 at 8:07 pm

    I have a question.How we can retrieve specific images from firebase storage by providing some parameters.
    Like i say i only need those images whose name is XYZ.

    Reply
  38. Dangdat Raymond says

    August 8, 2018 at 9:13 pm

    You are the best and most generous programmer. You will always be at the top. Thank you so much

    Reply
  39. Paul says

    August 10, 2018 at 11:59 am

    Images are not displayed in recyclerView please help me.
    Thanks!

    Reply
  40. Serenity says

    August 16, 2018 at 7:28 am

    Really Nice And Informative tutorial God Bless!!! Please just one more request could be greatly appreciated which is how to download the image back to our internal storage. Thanks

    Reply
  41. sagar says

    September 3, 2018 at 7:01 pm

    cannot see my image in the recycler view retriving from firebase

    Reply
  42. Siddharth J says

    November 26, 2018 at 12:02 pm

    Hello! I am not able to see the images in the recycler view.

    Reply

Leave a Reply to KIVANÇ Cancel reply

Your email address will not be published. Required fields are marked *

Search




Download our Android App

Simplified Coding in Google Play

About Me

Belal Khan

Hello I am Belal Khan, founder and owner of Simplified Coding. I am currently pursuing MCA from St. Xavier's College, Ranchi. I love to share my knowledge over Internet.

Connect With Me

Follow @codesimplified
Simplified Coding

Popular Tutorials

  • Android JSON Parsing – Retrieve From MySQL Database
  • Android Login and Registration Tutorial with PHP MySQL
  • Android Volley Tutorial – Fetching JSON Data from URL
  • Android Upload Image to Server using Volley Tutorial
  • Android TabLayout Example using ViewPager and Fragments
  • Retrieve Data From MySQL Database in Android using Volley
  • Firebase Cloud Messaging Tutorial for Android
  • Android Volley Tutorial – User Registration and Login
  • Android Upload Image to Server Using PHP MySQL
  • Android Navigation Drawer Example using Fragments




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

Quick Links

  • Advertise Here
  • Privacy Policy
  • Disclaimer
  • About
  • Contact Us
  • Write for Us

Categories

Android Advance Android Application Development Android Beginners Android Intermediate Ionic Framework Tutorial JavaScript Kotlin Android Others PHP Advance PHP Tutorial React Native

Copyright © 2017 · Simplified Coding· All rights Reserved. And Our Sitemap.All Logos & Trademark Belongs To Their Respective Owners·

loading Cancel
Post was not sent - check your email addresses!
Email check failed, please try again
Sorry, your blog cannot share posts by email.