如何从 Firebase 存储获取下载 url? [英] How to get the download url from Firebase Storage?

查看:38
本文介绍了如何从 Firebase 存储获取下载 url?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从 Firebase 的 uploadTask.addOnProgressListener 方法获取下载 URL.如何使用以下代码获取下载网址?

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.

推荐答案

Edit 2019 年 8 月 22 日:

最近在 Android SDK 的 StorageReference 类中添加了一个新方法,名为 list().

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().

所以现在当调用 getDownloadUrl()StorageReference 对象,它返回一个 Task 对象,并且 不再 返回一个 Uri 对象.

So now when calling getDownloadUrl() on a StorageReference object it returns a Task object and not an Uri object anymore.

还请记住,如果您的设备无法访问 Firebase 存储后端,则不会调用成功侦听器和失败侦听器(如果您打算使用它).只有在数据提交到 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 存储获取下载 url?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆