如何从Firebase Storage获取下载URL? [英] How to get the download url from Firebase Storage?
问题描述
我想从Firebase的uploadTask.addOnProgressListener
方法获取下载地址.如何使用以下代码获取下载网址?
I want to get the Download Url from uploadTask.addOnProgressListener
method of Firebase. How can I get the Download Url using following code?
UploadTask uploadTask = storageRef.putBytes(data);
uploadTask.addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>()
{
@Override
public void onProgress(UploadTask.TaskSnapshot taskSnapshot)
{
Log.d("aaaaasessin",""+taskSnapshot.getTask().getResult());
}
});
我使用了taskSnapshot.getTask().getResult()
,但是没有用.
I used taskSnapshot.getTask().getResult()
but that is not working.
推荐答案
编辑2019年8月22日:
Android SDK中最近在StorageReference的类中添加了一种名为
There is a new method recently added to StorageReference's class in the Android SDK named list().
要解决此问题,您需要遍历ListResult
并调用getDownloadUrl()
以获取每个文件的下载URL.请记住,getDownloadUrl()
方法是异步的,因此它返回一个Task
对象.请参阅下面的详细信息.
To solve this, you need to loop over the ListResult
and call getDownloadUrl()
to get the download URLs of each file. Rememeber that getDownloadUrl()
method is asynchronous, so it returns a Task
object. See below details.
要获取下载网址,您需要使用addOnSuccessListener
,如以下代码行所示:
In order to get the download url, you need to use addOnSuccessListener
, like in the following lines of code:
uploadTask.addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
storageRef.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
@Override
public void onSuccess(Uri uri) {
String url = uri.toString();
//Do what you need to do with url
}
});
}
});
在2018年5月23日的 Firebase发行说明中提到
As in the Firebase release notes on May 23, 2018 is mentioned that:
云存储版本16.0.1
Cloud Storage version 16.0.1
删除了不推荐使用的StorageMetadata.getDownloadUrl()和UploadTask.TaskSnapshot.getDownloadUrl()方法.要获取当前的下载URL,请使用StorageReference.getDownloadUr().
Removed the deprecated StorageMetadata.getDownloadUrl() and UploadTask.TaskSnapshot.getDownloadUrl() methods. To get a current download URL, use StorageReference.getDownloadUr().
现在,当调用在 StorageReference 对象,它将返回Task
对象,而不再不再一个Uri对象.
So now when calling getDownloadUrl() on a StorageReference object it returns a Task
object and not an Uri object anymore.
也请记住,如果您的设备无法访问Firebase Storage后端,则不会调用成功侦听器或失败侦听器(如果您打算使用它).仅在数据提交到Firebase服务器或被Firebase服务器拒绝后,才会调用成功/失败侦听器.
Please also rememeber, neither the success listener nor the failure listener (if you intend to use it), will be called if your device cannot reach Firebase Storage backend. The success/failure listeners will only be called once the data is committed to, or rejected by the Firebase servers.
这篇关于如何从Firebase Storage获取下载URL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!