无法写入Firebase Storage onSuccess侦听器内的Firebase数据库

时间:2017-04-13 15:02:59

标签: android firebase firebase-realtime-database firebase-storage

我正在尝试存储我将文件上传到Firebase存储后获得的Firebase存储URL,但由于我无法理解的原因,我无法写入Storage onSuccess内Firebase数据库中的某个位置监听器。有什么想法吗?如果这不是存储上传文件的URL位置的正确方法,那么是什么?

我似乎无法找到这样的例子。这是为Android。 谢谢

2 个答案:

答案 0 :(得分:1)

你想要完成的是完全可行的,这是一个例子

    StorageReference fileRef = ....;
    Uri fileUri = ....;
    UploadTask uploadTask = fileRef.putFile(fileUri);

    uploadTask.addOnFailureListener(new OnFailureListener() {
        @Override
        public void onFailure(@NonNull Exception exception) {
            // Handle unsuccessful uploads
            Log.d("TAG", "onFailure: " + exception);

        }
    }).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
        @Override
        public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
       // This is the URL you are looking for, here you should insert it into your db instance...Something like this
         Uri downloadUrl = taskSnapshot.getDownloadUrl();

         DatabaseReference dbRef = FirebaseDatabase.getInstance().getReference(); 
         dbRef.child("uploadUrls").child("url1").setValue(downloadUrl.toString());
        }
    });

我希望这会有所帮助

答案 1 :(得分:0)

 DatabaseReference dbRef = FirebaseDatabase.getInstance().getReference();

 uploadTask.addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception exception) {
                // Handle unsuccessful uploads
                Log.d("TAG", "onFailure: " + exception);

            }
        }).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
            @Override
            public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
           // This is the URL you are looking for, here you should insert it into your db instance...Something like this
             Uri downloadUrl = taskSnapshot.getDownloadUrl();

            addData(downloadUrl.toString());

            }
        });


public void addData(String downloadUrl)
{

dbRef.child("uploadUrls").child("url1").setValue(downloadUrl);

}