5

I am doing this Android Project in which i want help in retrieving all the URLs which are stored in Firebase Storage and display them all in imageview. The Problem with the given code is that it only fetches one download url.In what way can i get all the URLs for all the images stored in Firebase. In short: There is one activity in which I am saving the images to firebase and in other i want to retrieve all the images in Imageview.In what way i can do it ? Thanks

if(requestCode== GALLERY_INTENT && resultCode== RESULT_OK)
    {
          mProgress.setMessage("Uploading....");
          mProgress.show();
          Uri uri =data.getData();
        StorageReference filepath = mStorage.child("Photos").child(uri.getLastPathSegment());
        filepath.putFile(uri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
            @Override
            public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {

                mProgress.dismiss();
                Uri downloadUri = taskSnapshot.getDownloadUrl();

                Picasso.with(MainActivity.this).load(downloadUri).fit().centerCrop().into(mImageView);
                Toast.makeText(MainActivity.this,"Upload done",Toast.LENGTH_LONG).show();
            }
        }).addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception e) {
                Toast.makeText(MainActivity.this,"Upload  failed",Toast.LENGTH_LONG).show();
            }
        });
    }
AL.
  • 36,815
  • 10
  • 142
  • 281
Rohit
  • 69
  • 1
  • 8

2 Answers2

2

I don't think there is an API to fetch all files stored in firebase storage. When i was saving files , i used to store the file metadata including the file name, download url etc in the real time database.

So when i had to fetch all the files from the storage, i would use the database.

Edit :

Here is how i did it.

UploadTask uploadTask = storageRef.putBytes(data,storageMetadata);
    uploadTask.addOnFailureListener(new OnFailureListener() {
        @Override
        public void onFailure(@NonNull Exception exception) {

        }
    }).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
        @Override
        public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
             //Save to database using
         //taskSnapshot.getMetadata().getCustomMetadata(StringKeys.SOME_KEY);
        }
    }).addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() {
        @Override
        public void onProgress(UploadTask.TaskSnapshot taskSnapshot) {

        }
    });
Dishonered
  • 8,449
  • 9
  • 37
  • 50
0

In Firebase, currently there are no API call that can give you list of all files in Firebase Storage. Instead what you can do is, Once image is uploaded to Firebase Storage, get the download link of that image using getDownloadUrl(); and save it to real-time database.


To read or write data from the database, you need an instance of DatabaseReference:

private DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference();

To save link of the uploaded image to real time database, you need to create java object such as:

public class Image implements Serializable {
    public String downloadUrl;

    public Image() {
    }

    public Image(String dUrl) {
        this.downloadUrl = dUrl;
    }
}

Once you created java object, Add following lines of code in onSuccess(UploadTask.TaskSnapshot taskSnapshot):

Uri downloadUri = taskSnapshot.getDownloadUrl();
Image image = new Image(downloadUri.toString());
String userId = databaseReference.push().getKey();
databaseReference.child(userId).setValue(image);

This will add download link of that image in real-time database. Now when you want list of all the images in your Firebase Storage, simply use the following code:

private ArrayList<Data> list;
...
private void getImagesList() {
    databaseReference.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            for (DataSnapshot childSnapshot : dataSnapshot.getChildren()) {
                Data data = new Data(childSnapshot.child("downloadUrl").getValue().toString());
                list.add(data);
            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
        }
    });
}
Harshil
  • 914
  • 1
  • 12
  • 26