Android - Firebase - TaskSnapshot - 只能在私有范围内访问方法

时间:2017-06-12 01:14:17

标签: android firebase firebase-realtime-database

我的onSuccessListener()中有以下代码片段,用于保存我上传文件的下载URL。

 DatabaseReference downloadURLRef = rootRef.child("Child").child(value);
 Uri downloadUrl = taskSnapshot.getDownloadUrl();
 downloadURLRef.setValue(downloadUrl);

我收到此错误:

Error TaskSnapshot - Method should only be accessed within private scope while using android studio 2.3.

当我关注this post并停止警告时,我的应用在上传时崩溃,上传失败。 Here's the error for that.

是什么给出的?这也直接来自Firebase文档。

编辑:上传代码。

    storage = FirebaseStorage.getInstance();
    storageRef = storage.getReference(date + "/" + filename);
    uploadTask = storageRef.putFile(Uri.fromFile(file));

    // Register observers to listen for when the download is done or if it fails
    uploadTask.addOnFailureListener(new OnFailureListener() {
        @Override
        public void onFailure(@NonNull Exception exception) {
            // Handle unsuccessful uploads
        }
    }).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
        @Override
        public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {

            // TODO: Fix this bullshit
            DatabaseReference downloadURLRef = rootRef.child("Child").child(value);
            Uri downloadUrl = taskSnapshot.getDownloadUrl();
            downloadURLRef.setValue(downloadUrl);
        }
    });

1 个答案:

答案 0 :(得分:1)

downloadUrl保存为字符串,而不是Uri对象:

    DatabaseReference downloadURLRef = rootRef.child("Child").child(value);
    Uri downloadUrl = taskSnapshot.getDownloadUrl();
    downloadURLRef.setValue(downloadUrl.toString()); // <= CHANGE