我的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);
}
});
答案 0 :(得分:1)
将downloadUrl
保存为字符串,而不是Uri
对象:
DatabaseReference downloadURLRef = rootRef.child("Child").child(value);
Uri downloadUrl = taskSnapshot.getDownloadUrl();
downloadURLRef.setValue(downloadUrl.toString()); // <= CHANGE