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?
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?
Currently, there's no available API to handle multiple file uploading or downloading from Firebase Storage.
Check the following workarounds:
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;
}
});
}
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)
}
}
}
}