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

问题描述

要将图像上传到 Firebase 存储,我将 addOnSuccessListener 附加到 StorageReference 的实例上.在覆盖 onSuccess 方法时,我在 taskSnapshot 的实例上调用 getDownloadUrl() 但它给了我一个错误说

To upload images to Firebase Storage I am attaching addOnSuccessListener on the instance of the StorageReference. While overriding onSuccess method I am calling getDownloadUrl() on the instance of taskSnapshot but it is giving me an error saying

无法解析方法 getDownloadUrl()

这个应用程序是我 2 个月前创建的,之前这个应用程序运行良好,getDownloadUrl() 也运行良好.此外,在 taskSnapshot 实例中,当我按 Ctrl+space 时,在建议中我找不到 getDownloadUrl() 方法.为什么会这样?

This app I had created 2 months ago, earlier this app was working fine and getDownloadUrl() was working fine as well. Also, in taskSnapshot instance when I press Ctrl+space, in the suggestions I don't find getDownloadUrl() method. Why is it happening?

代码到onActivityResult():

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if(requestCode == RC_SIGN_IN) {
        if (resultCode == RESULT_OK) {
            Toast.makeText(this, "Signed in!!!1", Toast.LENGTH_SHORT).show();
        } else if (resultCode == RESULT_CANCELED) {
            Toast.makeText(this, "Failed to sign in", Toast.LENGTH_SHORT).show();
            finish();
        }
    }
    else if(requestCode == RC_PHOTO_PICKER && resultCode == RESULT_OK){
        Uri selectedPhoto = data.getData();

        StorageReference localRefrence = storageReference.child(selectedPhoto.getLastPathSegment());

        //  Uploading the file on the storage
        localRefrence.putFile(selectedPhoto).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
            @Override
            public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                 Uri downloadUrl = taskSnapshot.getDownloadUrl();

                FriendlyMessage message = new FriendlyMessage(mUsername, null, downloadUrl.toString());
                databaseReference.push().setValue(message);
            }
        });
    }
}

推荐答案

Firebase API 有 改变了.

The Firebase API has changed.

2018 年 5 月 23 日

云存储版本 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().

UploadTask.TaskSnapshot 有一个名为 getMetadata() 返回一个 StorageMetadata 对象.

这个StorageMetadata 对象包含一个名为 getReference() 返回一个 StorageReference 对象.

那个 StorageReference 对象包含 getDownloadUrl() 方法,现在返回一个 Task 对象而不是 Uri 对象.

That StorageReference object contains the getDownloadUrl() method, which now returns a Task object instead of an Uri object.

然后必须侦听此任务以获取 Uri,这可以异步或以阻塞方式完成;请参阅 Tasks API.

This Task must then be listened upon in order to obtain the Uri, which can be done asynchronously or in a blocking manner; see the Tasks API for that.

这篇关于Firebase Storage getDownloadUrl() 方法无法解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-30 08:31