简体   繁体   English

使用Firebase异步上传多张照片

[英]Asynchronous multiple photos upload with Firebase

Can anyone help me please. 谁能帮我。

I have a problem uploading multiple photos with Firebase. 我在使用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 我的问题是,当我循环浏览要上传的照片时,UploadTask似乎在单独的线程上工作,因此循环迭代所有照片,而第一张照片仍在上传,我想一个一个地上传它们,这样循环就不会迭代到下一张照片,除非已上传第一张照片

@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);
    }
}

I can give you an for the algorithm (just answered it for JS): 我可以给您一个算法(只需为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. 您可以使用Android Tasks.whenAlldocsblog post )做类似的事情:创建一个任务数组,然后使用whenAll()启动它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM