How to get a list of all files in Cloud Storage in a Firebase app?

后端 未结 19 1011
傲寒
傲寒 2020-11-21 11:25

I\'m working on uploading images, everything works great, but I have 100 pictures and I would like to show all of them in my View, as I get the complete list of

19条回答
  •  情书的邮戳
    2020-11-21 11:51

    You can use the following code. Here I am uploading the image to firebase storage and then I am storing the image download url to firebase database.

    //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() {
                            @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() {
                            @Override
                            public void onProgress(UploadTask.TaskSnapshot taskSnapshot) {
                                //displaying the upload progress 
                                double progress = (100.0 * taskSnapshot.getBytesTransferred()) / taskSnapshot.getTotalByteCount();
                                progressDialog.setMessage("Uploaded " + ((int) progress) + "%...");
                            }
                        });
    

    Now to fetch all the images stored in firebase database you can use

    //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();
                }
            });
    

    Fore more details you can see my post Firebase Storage Example.

提交回复
热议问题