本文介绍了错误:找不到 com.google.firebase.storage.UploadTask.TaskSnapshot 类型的符号方法 getDownloadUrl()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 gradle 更新之前一切正常,但后来出现了这个错误.我参考了官方文档,它提供了相同的代码.不接受 getDownloadUrl() 方法

Before gradle update everything worked fine, but later on this error popped up. I have referred to the official documents and it provides the same code.Not accepting the getDownloadUrl() method

我添加了最新的正确依赖项,并且 gradle 同步成功.app/build.gradle

I have added the correct dependency which is latest and the gradle sync is successful.app/build.gradle

这是 firebase 文档中提供的示例代码,与我的相同.Firebase 助手

This is the sample code provided in firebase docs which is same as mine.Firebase Assistant

即使所有必需的东西都存在,我也无法理解可能会出现什么问题.卡在这个问题上 2 天了,请帮忙!

I'm unable to understand what could possibly go wrong even if all required things are present. Stuck on this since 2 days, please help!

推荐答案

正如 Doug 所指出的,UploadTask.getDownloadUrl() 已被弃用,因此请使用 StorageReference.getDownloadUrl().

As Doug pointed out, UploadTask.getDownloadUrl() is deprecated, so use StorageReference.getDownloadUrl().

但是 StorageReference.getDownloadUrl() 返回 Task,必须异步处理,你不能Uri downloadUrl = photoRef.getDownloadUrl().getResult(); 否则你将得到 java.lang.IllegalStateException: 任务尚未完成

But StorageReference.getDownloadUrl() returns Task, which must be handled asynchronously, you cannot do Uri downloadUrl = photoRef.getDownloadUrl().getResult(); else you will get java.lang.IllegalStateException: Task is not yet complete

因此,像这样异步处理它

Therefore, handle it asynchronously like this

 photoRef.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
                    @Override
                    public void onSuccess(Uri uri) {
                        Uri downloadUrl = uri;
                        Toast.makeText(getBaseContext(), "Upload success! URL - " + downloadUrl.toString() , Toast.LENGTH_SHORT).show();
                    }
                });

这篇关于错误:找不到 com.google.firebase.storage.UploadTask.TaskSnapshot 类型的符号方法 getDownloadUrl()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-27 10:52