android - 如何从 StorageReference 获取下载 URL
问题描述
在我的项目中,我可以getDownloadUrl()
在上传文件后
ref.putFile(imgUri).continueWithTask(new Continuation<UploadTask.TaskSnapshot, Task<Uri>>() {
@Override
public Task<Uri> then(@NonNull Task<UploadTask.TaskSnapshot> task) throws Exception {
if (!task.isSuccessful()) {
if(task.getException() != null) {
throw task.getException();
}
}
return ref.getDownloadUrl();
}
}) ...
但是,由于我正在使用云功能调整图像大小并将其重新上传到存储桶,因此下载 url 会发生变化,因此知道图像位置的唯一方法是通过StorageReference
. 我怎么getDownloadUrl()
能从StorageReference
?我试过ref.getDownloadUrl().addOnSuccessListener
哪个不会被解雇并且ref.getDownloadUrl().addOnCompleteListener
会task.getResult()
引发错误。
解决方案
尝试使用这个:我试图存储图像
您可以从 task.getResult().toString() 获取下载 URL
final StorageReference ref = FirebaseStorage.getInstance().getReference().child("images").child(imageName)
uploadTask = ref.putBytes(data);
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();
String URL = task.getResult().toString();
} else {
// Handle failures
// ...
}
}
});
推荐阅读
- java - Springdocs - 为 REST 端点生成供应商扩展
- react-native - React Native 中 FlatList 内的 SlidingUpPanel 按钮
- sql - 在oracle中根据月份获取每个季度的数据
- .net-core - .net core 3.1 Bearer error="invalid_token", error_description="观众 'empty' 无效"
- javascript - 在javascript中按比例增加这些数字的数学正确方法是什么?
- php - 550 无法将目录更改为公共/存储:没有这样的文件或目录 filezilla
- java - 错误:无法解析符号 'DateOperators' - MongoTemplate
- python - 简单的模糊器在 python 3 中不起作用,但在 python 2 中起作用
- flutter - 我如何知道何时在 TextFormField 上按下键盘键?
- c# - 如何在 C# 中使用 http 请求获取本地和会话存储值?