如何在Android Firebase上获取上传的多个图像的下载URL

问题描述 投票:0回答:1

我在此站点上看到了同样的问题。但是大多数问题与查找单个上传图像的下载网址有关。从这些帖子中寻求帮助,现在我可以获得单个图像的下载URL。

但是当我尝试获取多个图像的下载URL时,我遇到了问题。我想做三件事...

1。选择三张图像2.将它们上传到Firebase存储3.获取上传的图像的URL并将其保存在Arraylist中。

我可以成功地完成前两项,但是不能完成第三项。当我单击“更新”按钮时,所有图像均完美存储在Firebase存储器中,但在获取所有图像的下载URL时显示错误。

这里是我单击“更新”按钮时的代码。

upload.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                progressDialog.setMessage("Uploading .... ");
                progressDialog.show();

                storageReference = FirebaseStorage.getInstance().getReference().child("Pictures");

                int uploadCount = 0;

//imageList is the Uri type Array List which holds the address of selected 3 images. 
//imageAddress is that String type ArrayList where i want to save all downloadUrls of images(each url is saved as a string).
// imagePath is StorageReference Type

                while(uploadCount < imageList.size()) {
                    Log.d("UploadCount", uploadCount+"");
                    Uri uri_Image = imageList.get(uploadCount); 
                    imagePath = storageReference.child(uri_Image.getLastPathSegment());

                    imagePath.putFile(uri_Image).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                        @Override
                        public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                            imagePath.getDownloadUrl().addOnSuccessListener(newOnSuccessListener<Uri>() {
                                @Override
                                public void onSuccess(Uri uri) {
                                    Uri downloadUri = uri;
                                    imageAddress.add(downloadUri.toString());
                                    Log.d("ImageAddress Size: ", imageAddress.size()+"");
                                }
                            });
                        }
                    }).addOnFailureListener(new OnFailureListener() {
                        @Override
                        public void onFailure(@NonNull Exception e) {
                            Toast.makeText(SignOutActivity.this, e.getMessage(), Toast.LENGTH_LONG).show();
                            progressDialog.dismiss();
                        }
                    }); //.............
                    if(uploadCount == (imageList.size()-1)) {
                        Log.d("Good", "HELLO HELLO");
                        Toast.makeText(SignOutActivity.this, "Successfully Uploaded", Toast.LENGTH_LONG).show();
                        upload.setClickable(false);
                        progressDialog.dismiss();
                    }
                    else {
                        Log.d("BAD", "NOT HELLO "+uploadCount);
                    }
                    uploadCount = uploadCount + 1;

                }
            }
        });

这里是错误

2020-02-15 17:02:26.945 28207-28735/com.example.practiceapplication E/StorageException: StorageException has occurred.
    Object does not exist at location.
     Code: -13010 HttpResult: 404
2020-02-15 17:02:26.946 28207-28735/com.example.practiceapplication E/StorageException: {  "error": {    "code": 404,    "message": "Not Found.  Could not get object",    "status": "GET_OBJECT"  }}
    java.io.IOException: {  "error": {    "code": 404,    "message": "Not Found.  Could not get object",    "status": "GET_OBJECT"  }}
        at com.google.firebase.storage.network.NetworkRequest.parseResponse(com.google.firebase:firebase-storage@@19.1.1:433)
        at com.google.firebase.storage.network.NetworkRequest.parseErrorResponse(com.google.firebase:firebase-storage@@19.1.1:450)
        at com.google.firebase.storage.network.NetworkRequest.processResponseStream(com.google.firebase:firebase-storage@@19.1.1:441)
        at com.google.firebase.storage.network.NetworkRequest.performRequest(com.google.firebase:firebase-storage@@19.1.1:272)
        at com.google.firebase.storage.network.NetworkRequest.performRequest(com.google.firebase:firebase-storage@@19.1.1:286)
        at com.google.firebase.storage.internal.ExponentialBackoffSender.sendWithExponentialBackoff(com.google.firebase:firebase-storage@@19.1.1:70)
        at com.google.firebase.storage.internal.ExponentialBackoffSender.sendWithExponentialBackoff(com.google.firebase:firebase-storage@@19.1.1:62)
        at com.google.firebase.storage.GetDownloadUrlTask.run(com.google.firebase:firebase-storage@@19.1.1:76)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
        at java.lang.Thread.run(Thread.java:761)
2020-02-15 17:02:30.712 28207-28207/com.example.practiceapplication D/ImageAddress Size:: 1

如果有人告诉我更正,这将对我非常有帮助。谢谢

我在此站点上看到了同样的问题。但是大多数问题与查找单个上传图像的下载网址有关。从这些帖子中获取帮助,现在我可以获取...

android image firebase firebase-storage
1个回答
0
投票

您的代码在处理异步代码和for循环时遇到了混合使用局部变量和共享全局变量的问题。

© www.soinside.com 2019 - 2024. All rights reserved.