在Firebase中上传多个图像

时间:2019-01-19 13:33:54

标签: android firebase firebase-storage

我正在尝试使用for循环上传多张图片。我已将所有文件列表中列出,并且在完成任务时,只有最后一张图像被上传了几次。

如果我要上传五张图片,那么最后一张图片将被上传五次。

我也尝试过将所有任务添加到列表中并循环执行。但是仍然只有最后一张图像被上传。

foreach(imgurl){

final StorageReference storageReference = mStorageReference.child("/photos" + "/photo" + (count + 1));

UploadTask uploadTask = storageReference.putFile(imgUrl);

Task<Uri> urlTask = uploadTask.continueWithTask(new Continuation<UploadTask.TaskSnapshot, Task<Uri>>() {
                        @Override
                        public Task<Uri> then(@NonNull Task<UploadTask.TaskSnapshot> task) throws Exception {
                            if (!task.isSuccessful()) {
                                throw task.getException();
                            }
                            return storageReference.getDownloadUrl();
                        }
                    }).addOnCompleteListener(new OnCompleteListener<Uri>() {
                        @Override
                        public void onComplete(@NonNull Task<Uri> task) {
                            if (task.isSuccessful()) {
                                Uri downloadUri = task.getResult();
                                addPhoto(downloadUri.toString());

                            } else {
                                Log.d(TAG, "onFailure: Photo upload failed.");
                                Toast.makeText(mContext, "Photo upload failed ", Toast.LENGTH_SHORT).show();
                            }
                        }
                    });

}

}

2 个答案:

答案 0 :(得分:0)

如果您有5张图片,那么这5张图片应该包含一个不同的imgUrl,您将N次使用相同的uploadTask迭代到Firebase Storage的上传代码

UploadTask uploadTask = storageReference.putFile(imgUrl); --> This same task

tasks.add(uploadTask);

for (UploadTask str : tasks) { --> is repeating N times with the same imgUrl

Task<Uri> urlTask = str.continueWithTask(new Continuation<UploadTask.TaskSnapshot, Task<Uri>>() {
                        @Override
                        public Task<Uri> then(@NonNull Task<UploadTask.TaskSnapshot> task) throws Exception {
                            ...

您应该做的是移动forEach循环以处理每个不同的imgUrl,然后将每个上传到您的存储设备中,例如

伪代码

forEach(imgUrl) {
   uploadtask = storageReference.putFile(imgUrl);
   tasks.add(uploadTask);

   Task<Uri> urlTask = str.continueWithTask(new Continuation<UploadTask.TaskSnapshot, Task<Uri>>() {
                        @Override
                        public Task<Uri> then(@NonNull Task<UploadTask.TaskSnapshot> task) throws Exception {
                            ...

答案 1 :(得分:0)

您可以尝试一下,它对我有用!

private void uploadImages() {
    findViewById(R.id.add).setEnabled(false);
    findViewById(R.id.add).setBackgroundResource(R.drawable.border_buttons_disable);
    builder = new AlertDialog.Builder(context);
    builder.setTitle("Adding " + type);
    builder.setMessage("We are validating data...");
    alertDialog = builder.create();
    alertDialog.setCancelable(false);
    alertDialog.setCanceledOnTouchOutside(false);
    alertDialog.show();
    for (int i = 0; i < urls.size(); i++) {
        UploadTask uploadTask = (UploadTask) storageReference.child("images/businesses/products/" + preferencesAdapter.getId() + "-" + _sku + "-" + i + ".png").putFile(Uri.parse(urls.get(i))).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
            @Override
            public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                taskSnapshot.getStorage().getDownloadUrl().addOnCompleteListener(new OnCompleteListener<Uri>() {
                    @Override
                    public void onComplete(@NonNull Task<Uri> task) {
                        selectedPictures.add(task.getResult().toString().substring(0, task.getResult().toString().indexOf("&token")));
                        if (selectedPictures.size() == urls.size()) {
                            alertDialog.dismiss();
                            addProduct();
                        }
                    }
                });
            }
        });
    }
}

只需创建以下数组: ArrayList<String> urls;

对于每个图像用户,请从意图中选择添加到“网址”