【发布时间】: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 都加载了相同的图片,虽然它们不一样,但它们看起来像这样,
我如何获得两个 URL 来加载我上传的两张图片,而不仅仅是一张
感谢您的帮助,非常感谢。
【问题讨论】:
-
我从我的数据库中删除了该集合并从一开始就添加了新条目,现在它对我的一些用户来说很好,但对于其他用户来说,封面 URL 为空
标签: android firebase google-cloud-firestore firebase-storage