Mahmoud Amin
Mahmoud Amin

Reputation: 36

Asynchronous multiple photos upload with Firebase

Can anyone help me please.

I have a problem uploading multiple photos with Firebase. My problem is that when I loop through the photos to be uploaded the UploadTask seems to work on a separate thread so the loop iterates through all photos while the first photo still uploads I want to upload them one by one so the loop don't iterate to the next photo unless the first photo have been uploaded

@Override
public boolean onOptionsItemSelected(final MenuItem item) {
    switch(item.getItemId()){
        case R.id.add_photo_option:
            item.setEnabled(false);
            final ProgressDialog dialog = new ProgressDialog(getActivity());
            dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
            for (int x = 0; x < photosList.getAdapter().getItemCount(); x++){
                int photoNumber = x+1;
                dialog.setTitle("Uploading photo no: " + photoNumber);
                dialog.show();
                ImageAdapter adapter = (ImageAdapter) photosList.getAdapter();
                Uri photoUri = adapter.getPhotoUri(x);
                String path = "photos/" + UUID.randomUUID() + ".png";
                StorageReference storage = mStorageRef.getReference(path);
                UploadTask task = storage.putFile(photoUri);
                task.addOnProgressListener(getActivity(), new OnProgressListener<UploadTask.TaskSnapshot>() {
                    @Override
                    public void onProgress(UploadTask.TaskSnapshot taskSnapshot) {
                        long progress = taskSnapshot.getBytesTransferred();
                        long totalBytes = taskSnapshot.getTotalByteCount();
                        dialog.setMessage("Uploading: " + progress + " KBs out of " + totalBytes + " KBs");
                    }
                });
                task.addOnSuccessListener(getActivity(), new OnSuccessListener<UploadTask.TaskSnapshot>() {
                    @Override
                    public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                        dialog.setMessage("Uploaded");
                        item.setEnabled(true);
                        Toast.makeText(getActivity(), taskSnapshot.getDownloadUrl().toString(), Toast.LENGTH_SHORT).show();
                    }
                });
            }
            dialog.dismiss();
            return true;
        default:
        return super.onOptionsItemSelected(item);
    }
}

Upvotes: 0

Views: 1768

Answers (1)

Mike McDonald
Mike McDonald

Reputation: 15953

I can give you an for the algorithm (just answered it for JS):

// set it up
firebase.storage().ref().constructor.prototype.putFiles = function(files) { 
  var ref = this;
  return Promise.all(files.map(function(file) {
    return ref.child(file.name).putFile(file);
  }));
}

// use it!
firebase.storage().ref().putFiles(files).then(function(metadatas) {
  // Get an array of file metadata
}).catch(function(error) {
  // If any task fails, handle this
});

You can use the Android Tasks.whenAll (docs, blog post) to do a similar thing: create an array of tasks, then use a whenAll() to kick them off.

Upvotes: 1

Related Questions