本文介绍了taskSnapshot.getDownloadUrl() 已弃用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

到目前为止,从 Firebase 存储文件中获取 url 的方法,我曾经这样做过taskSnapshot.getDownloadUrl,但现在不推荐使用,我应该使用哪种方法?

Until now, the way to get the url from file on Storage in Firebase, I used to do thistaskSnapshot.getDownloadUrl, but nowadays is deprecated, which method I should use?

推荐答案

正如 Doug 所说,你需要在 Task 中运行它

As Doug says, you will need to run it inside a Task

这里有一个关于你需要如何实现它的提示

Here is a hint of how you need to implement it

final StorageReference ref = storageRef.child("your_REF");
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();
                String downloadURL = downloadUri.toString();
            } else {
                // Handle failures
                // ...
            }
        }
    });

关于如何实现它的更多信息,你可以查看这个在我回答这个问题后 7 天打开的 github 问题 https://github.com/udacity/and-nd-firebase/issues/41

For more information about how to implement it, you can check this github question that was opened 7 days after I answered this https://github.com/udacity/and-nd-firebase/issues/41

这篇关于taskSnapshot.getDownloadUrl() 已弃用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-27 10:52