5

I want to upload and download images from Firebase Storage and show it in a RecyclerView. I can upload and download one image at a time, but I can't with multiple images.

How can I do this?

CopsOnRoad
  • 237,138
  • 77
  • 654
  • 440
Shailendra Kushwah
  • 415
  • 1
  • 6
  • 17

3 Answers3

2

Currently, there's no available API to handle multiple file uploading or downloading from Firebase Storage.

Check the following workarounds:

  1. https://stackoverflow.com/a/37337436/6523173
  2. https://stackoverflow.com/a/37849978/6523173
Community
  • 1
  • 1
looptheloop88
  • 3,026
  • 17
  • 20
0
private void uploadMultipleFile(final int  index){
    Uri resultUri=Uri.fromFile(new File(errorImageStoredPaths.get(index)));
    StorageReference riversRef = storageReference.child("images/"+resultUri.getLastPathSegment());
    UploadTask uploadTask = riversRef.putFile(resultUri);
    uploadTask.addOnFailureListener(exception -> {
    }).addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() {
        @Override
        public void onProgress(@NonNull UploadTask.TaskSnapshot snapshot) {
            showLog("Uploading...screens "+index);
        }
    }).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
        @Override
        public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
            showLog("Upload screen success");
            riversRef.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
                @Override
                public void onSuccess(Uri uri) {
                uploadedImages.add(uri.toString());
                }
            });
            if((errorImageStoredPaths.size() - 1) != index){
                uploadMultipleFile(index+1);
            }else {
                showLog("All upload over");
                uploadToFireStore();
            }
        }
    }).addOnFailureListener(new OnFailureListener() {
        @Override
        public void onFailure(@NonNull Exception e) {
            showLog("Upload failed");
            return;
        }
    });
}
0
val storage = Firebase.storage
val storageRef = storage.reference
val imagesRef: StorageReference = storageRef.child("imgs")
val files = listOf<File>()
    
//upload 10 files, wait until they are uploaded, then upload the next 10
uploadMultipleFiles(imagesRef, files, batchSize = 10)

suspend fun uploadMultipleFiles(
    ref: StorageReference,
    files: List<File>,
    batchSize: Int = 10,
) {
    files.chunked(batchSize).forEach { chunk ->
        for (file in chunk) {
            var iscomplete = false
            ref.child(file.name).putBytes(file.readBytes())
                .addOnCompleteListener {
                    iscomplete = true
                }
            while (iscomplete.not()) {
                delay(20)
            }
        }
    }
}

yazan sayed
  • 777
  • 7
  • 24