我如何按照arraylist的顺序将多个文件上传到Firebase数据库? [英] how do I upload multiple files to firebase databse in order as in arraylist?

查看:40
本文介绍了我如何按照arraylist的顺序将多个文件上传到Firebase数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将多个图像添加到Firebase,但似乎没有按顺序上传.我相信myUrlList是根据上传到服务器的顺序添加的.有什么办法可以对myUrlsList和imageUrlList进行排序?

I'm trying to add multiple images to firebase, but it seems like it doesn't upload in order. I believe myUrlList gets added depending on the order of getting uploaded to the server. Is there any way I can sort myUrlsList same as imageUrlList?

    for(int i=0; i< imageUriList.size(); i++){
            final StorageReference filereference = storageReference.child(System.currentTimeMillis()
                    + "." + getFileExtension(imageUriList.get(i)));

            uploadTask = filereference.putFile(imageUriList.get(i));
            uploadTask.continueWithTask(new Continuation() {
                @Override
                public Object then(@NonNull Task task) throws Exception {
                    if(!task.isSuccessful()){
                        throw task.getException();
                    }

                    return filereference.getDownloadUrl();
                }
            }).addOnCompleteListener(new OnCompleteListener<Uri>() {
                @Override
                public void onComplete(@NonNull Task<Uri> task) {
                    if(task.isSuccessful()){

                        Uri downloadUri = task.getResult();
                        myUrl = downloadUri.toString();
                        myUrlList.add(myUrl);

推荐答案

即使您使用循环,并且从理论上讲,图像的上传也应按照迭代的顺序进行,您不知道要花多少时间才能完成.实际将每个图像分别上传到Firebase存储.正如@DougStevenson在评论中提到的那样,您正在并行"上传所有内容.因此,即使较小的图像紧接在较大的图像之后,较小尺寸的图像也可以比较大尺寸的图像更快地上载,因为所需的上载时间更少.

Even if you are using a loop and theoretically, the upload of the images should be in the order of the iteration, you cannot know how much it will take to actually upload each image to the Firebase Storage separately. As @DougStevenson mentioned in his comment, you are uploading everything in "parallel". So an image of a smaller size can be uploaded much faster than an image of a bigger size, even if the smaller image is positioned right after the larger image, as it will take a smaller amount of time to be uploaded.

解决此问题的解决方案是等到上一张图像的上传完成后,再等到一张图像被上传并开始下一次上传.通常,这是通过递归来完成的,方法是调用自身.

The solution to solve this issue is to wait until an image is uploaded and start the next upload, right after the upload of the previous image completes. This is typically done with recursion, with a method that invokes itself.

private void uploadImageToFirebaseStorage() {
    if (imageUriList.size() > 0) {
        Uri imageUri = imageUriList.get(0);
        StorageReference filereference = storageReference.child(System.currentTimeMillis()
                + "." + getFileExtension(imageUri));
        imageUriList.remove(0);
        uploadTask = filereference.putFile(imageUri);
        uploadTask.continueWithTask(new Continuation() {
            @Override
            public Object then(@NonNull Task task) throws Exception {
                if(!task.isSuccessful()){
                    throw task.getException();
                }

                return filereference.getDownloadUrl();
            }
        }).addOnCompleteListener(new OnCompleteListener<Uri>() {
            @Override
            public void onComplete(@NonNull Task<Uri> task) {
                if (task.isSuccessful()) {
                    Uri downloadUri = task.getResult();
                    myUrl = downloadUri.toString();
                    myUrlList.add(myUrl);
                    uploadBeerImageToFirebaseStorage(); //Call when completes
                }
            }
        });
    }
}

首先,使用uploadImageToFirebaseStorage()将其启动.上传图片后,该方法将检查是否还有更多工作要做,如果是这种情况,则重新调用自身.

First, kick it off with uploadImageToFirebaseStorage(). Once an image is uploaded, the method will check if there's more work that should be done, and reinvokes itself if that's the case.

这篇关于我如何按照arraylist的顺序将多个文件上传到Firebase数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆