英文:
How to get download url from uploaded files using Firebase
问题
阅读文档后,我发现有一个部分介绍了如何上传文件并获取其下载链接。
获取下载链接的代码如下:
Kotlin
val ref = storageRef.child("images/mountains.jpg")
uploadTask = ref.putFile(file)
val urlTask = uploadTask.continueWithTask { task ->
if (!task.isSuccessful) {
task.exception?.let {
throw it
}
}
ref.downloadUrl
}.addOnCompleteListener { task ->
if (task.isSuccessful) {
val downloadUri = task.result
} else {
// 处理失败情况
// ...
}
}
Java
final StorageReference ref = storageRef.child("images/mountains.jpg");
uploadTask = ref.putFile(file);
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 ref.getDownloadUrl();
}
}).addOnCompleteListener(new OnCompleteListener<Uri>() {
@Override
public void onComplete(@NonNull Task<Uri> task) {
if (task.isSuccessful()) {
Uri downloadUri = task.getResult();
} else {
// 处理失败情况
// ...
}
}
});
但是,哪一行代码用于获取下载链接?
是 "return ref.getDownloadUrl();" / "ref.downloadUrl" 吗?
还是 "Uri downloadUri = task.getResult();" / "val downloadUri = task.result;"?
英文:
Reading the documentation, I found a section talking about how to upload files and get its download link.
The code to get download link is:
Kotlin
val ref = storageRef.child("images/mountains.jpg")
uploadTask = ref.putFile(file)
val urlTask = uploadTask.continueWithTask { task ->
if (!task.isSuccessful) {
task.exception?.let {
throw it
}
}
ref.downloadUrl
}.addOnCompleteListener { task ->
if (task.isSuccessful) {
val downloadUri = task.result
} else {
// Handle failures
// ...
}
}
Java
final StorageReference ref = storageRef.child("images/mountains.jpg");
uploadTask = ref.putFile(file);
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();
}
// Continue with the task to get the download URL
return ref.getDownloadUrl();
}
}).addOnCompleteListener(new OnCompleteListener<Uri>() {
@Override
public void onComplete(@NonNull Task<Uri> task) {
if (task.isSuccessful()) {
Uri downloadUri = task.getResult();
} else {
// Handle failures
// ...
}
}
});
But what is the line I get the download URL?
Is it the "return ref.getDownloadUrl();"/"ref.downloadUrl"?
Or the "Uri downloadUri = task.getResult();
"/"val downloadUri = task.result
"?
答案1
得分: 1
在这两种情况下,名为 downloadUri
的变量是最终的下载URL。如果你愿意,你可以使用 downloadUri.toString()
将其转换为普通的字符串。
它不是 ref.getDownloadUrl()
的返回值。这是一个非常常见的错误。getDownloadUrl()
是异步的,不会立即返回URL。这就是为什么你需要回调函数。
另请参阅:https://stackoverflow.com/questions/37374868/how-to-get-url-from-firebase-storage-getdownloadurl
英文:
In both cases, the variable called downloadUri
is the final download URL. You can convert it to a plain old string, if you want, with downloadUri.toString()
.
It is not the return value of ref.getDownloadUrl()
. That is a very common mistake. getDownloadUrl()
is asynchronous and doesn't return the URL immediately. That's why you need the callback.
See also: https://stackoverflow.com/questions/37374868/how-to-get-url-from-firebase-storage-getdownloadurl
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论