【问题标题】:how to get download urls of multiple image uploads to firebase storage如何将多个图像上传的下载网址下载到 Firebase 存储
【发布时间】:2019-11-04 05:55:23
【问题描述】:

我正在将两个“用户选择的”图像上传到 Firebase 存储并获取它们的 URL 以保存到 Firestore,但 URL 仅保存指向其中一张图片,尽管它们看起来并不相同。 有一个类似的问题,但是关于 swift 并且它没有回答我的问题,这是该问题的 URL。 get download url from multiple file upload firebase storage

我尝试使用 ref.getDownloadUrl() 获取下载链接,但这给出了错误的 URL,并且 tasksnapshot.getDownloadUrl() 不再起作用。 我尝试阅读很多答案,但没有什么帮助大多数都过时的 tasksnapshot.getDownloadUrl() 方法。

        if (profileImageUri != null) {

            //upload first image
            profileImageRef.putFile(profileImageUri).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 profileImageRef.getDownloadUrl();
                }
            }).addOnCompleteListener(new OnCompleteListener<Uri>() {
                @Override
                public void onComplete(@NonNull Task<Uri> task) {
                    if (task.isSuccessful()) {
                        Uri downloadUri = task.getResult();
                        profileImageUrl = String.valueOf(downloadUri);
                        saveUserInfo(); // method to save the URLs along with other info
                    } else {
                        Toast.makeText(EditProfile.this, "upload failed: " + task.getException().getMessage(), Toast.LENGTH_SHORT).show();
                    }
                    btSaveInfo.setVisibility(View.VISIBLE);
                    progressBar.setVisibility(View.GONE);
                }
            });
        }

        if (coverImageUri != null) {

            // Upload second image
            profileImageRef.putFile(coverImageUri).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 profileImageRef.getDownloadUrl();
                }
            }).addOnCompleteListener(new OnCompleteListener<Uri>() {
                @Override
                public void onComplete(@NonNull Task<Uri> task) {

                    if (task.isSuccessful()) {
                        Uri downloadCUri = task.getResult();
                        coverImageUrl = String.valueOf(downloadCUri);
                        Toast.makeText(getApplicationContext(), "Cover Picture Uploaded", Toast.LENGTH_SHORT).show();
                    } else {
                        Toast.makeText(EditProfile.this, "Cover picture upload failed: " + task.getException().getMessage(), Toast.LENGTH_SHORT).show();
                    } 
                }
            });

上传了两张图片,我可以在我的 firebase 控制台中看到它们,但是保存在 firestore 中的 URL 都加载了相同的图片,虽然它们不一样,但它们看起来像这样,

  1. https://firebasestorage.googleapis.com/v0/b/sefnetapp-39b37.appspot.com/o/profilePics%2F1561103180500.jpg?alt=media&token=8dd5ee93-9bbc-46ee-89b9-fb5ae5b36128

  2. https://firebasestorage.googleapis.com/v0/b/sefnetapp-39b37.appspot.com/o/profilePics%2F1561103180500.jpg?alt=media&token=c153c59b-c6b3-428b-a845-3db3b19a38e3

我如何获得两个 URL 来加载我上传的两张图片,而不仅仅是一张

感谢您的帮助,非常感谢。

【问题讨论】:

  • 我从我的数据库中删除了该集合并从一开始就添加了新条目,现在它对我的一些用户来说很好,但对于其他用户来说,封面 URL 为空

标签: android firebase google-cloud-firestore firebase-storage


【解决方案1】:

我刚刚意识到上传封面图片后我没有保存我的信息,我也在封面图片上传方法的 onComplete 中添加了 saveInfo 方法,现在它工作正常。

    final StorageReference profileImageRef = FirebaseStorage.getInstance()
                .getReference(SefnetContract.PROFILE_PICS_REF + System.currentTimeMillis() + ".jpg");

        // Upload profile picture
        if (profileImageUri != null) {
            progressBar.setVisibility(View.VISIBLE);
            profileImageRef.putFile(profileImageUri).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 profileImageRef.getDownloadUrl();
                }
            }).addOnCompleteListener(new OnCompleteListener<Uri>() {
                @Override
                public void onComplete(@NonNull Task<Uri> task) {
                    if (task.isSuccessful()) {
                        Uri downloadUri = task.getResult();
                        profileImageUrl = String.valueOf(downloadUri);
                        saveUserInfo();
                    } else {
                        Toast.makeText(EditProfile.this, "upload failed: "
                                + task.getException().getMessage(), Toast.LENGTH_SHORT).show();
                    }
                    progressBar.setVisibility(View.GONE);
                }
            });
        }
        if (coverImageUri != null) {
            // Upload cover picture
            progressBar.setVisibility(View.VISIBLE);
            profileImageRef.putFile(coverImageUri).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 profileImageRef.getDownloadUrl();
                }
            }).addOnCompleteListener(new OnCompleteListener<Uri>() {
                @Override
                public void onComplete(@NonNull Task<Uri> task) {
                    progressBar.setVisibility(View.GONE);
                    if (task.isSuccessful()) {
                        Uri downloadCUri = task.getResult();
                        coverImageUrl = String.valueOf(downloadCUri);
                        saveUserInfo();
                        Toast.makeText(getApplicationContext(), "Cover Picture Uploaded", Toast.LENGTH_SHORT).show();
                    } else {
                        Toast.makeText(EditProfile.this, "Cover picture upload failed: "
                                + task.getException().getMessage(), Toast.LENGTH_SHORT).show();
                    }
                }
            });
        }

我仍然觉得可以以更好的方式完成,所以如果您知道比这更好的方法,请分享。我想要一种方法,我可以通过重复单击按钮来阻止用户多次初始化上传方法,谢谢

【讨论】:

  • 我将两个图像上传的方法分开并将图像保存在不同的参考文献中,例如封面/和简介/,它的工作原理
猜你喜欢
  • 2020-02-08
  • 1970-01-01
  • 2019-03-24
  • 2019-04-20
  • 1970-01-01
  • 1970-01-01
  • 2018-03-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多