如何将上传到Firebase云存储的图像的公共URL上传到实时数据库

问题描述 投票:0回答:1

将图像上传到Firebase Cloud Storage后,我想将其公共下载URL添加到实时数据库中的子节点。

我的文件上传代码

    private void uploadFile() {

        if (imageUri != null) {

            StorageReference fileRef = storageReference.child(UID + "/pic" + ".png");

            UploadTask uploadTask =  fileRef.putFile(imageUri);
            uploadTask.addOnSuccessListener(OrderPickupActivity.this, new OnSuccessListener<UploadTask.TaskSnapshot>() {
                @Override
                public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                    fileRef.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
                        @Override
                        public void onSuccess(Uri uri) {
                            imageURL = uri.toString();
                        }
                    });
                }
            }).addOnFailureListener(new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception e) {
                    Toast.makeText(OrderPickupActivity.this, e.toString(), Toast.LENGTH_LONG).show();
                }
            });

        } else {
            Toast.makeText(this, "Choose an image!", Toast.LENGTH_SHORT).show();
        }

    }

上传到实时数据库

uploadFile();

                    PickupDetails newPickup = new PickupDetails(
                            date,
                            time,
                            name,
                            email,
                            address,
                            pinCode,
                            UID,
                            phoneNumber,
                            imageURL
                    );

                    FirebaseDatabase.getInstance().getReference("Pending-Requests")
                            .child(FirebaseAuth.getInstance().getCurrentUser().getUid())
                            .setValue(newPickup)
                            .addOnCompleteListener(new OnCompleteListener<Void>() {
                                @Override
                                public void onComplete(@NonNull Task<Void> task) {
                                    //Toast.makeText(OrderPickupActivity.this, "Success!", Toast.LENGTH_LONG).show();
                                    progressBar.setVisibility(View.GONE);
                                    startActivity(new Intent(OrderPickupActivity.this, MainActivity.class));
                                }
                            })
                            .addOnFailureListener(new OnFailureListener() {
                                @Override
                                public void onFailure(@NonNull Exception e) {
                                    Toast.makeText(OrderPickupActivity.this, "Error, " + e, Toast.LENGTH_LONG).show();
                                    progressBar.setVisibility(View.GONE);
                                }
                            });

我已经使用了StackOverflow上类似问题的答案,但是似乎没有一个对我有帮助。当我记录结果时,我可以获取该值,但是它没有到​​达数据库。

java android firebase firebase-realtime-database firebase-storage
1个回答
0
投票

在您的getDownloadUrl侦听器的onSuccess方法中:

databaseRef.child("images").push().setValue(uri.toString())
    .addOnSuccessListener(new OnSuccessListener<Void>() {
        @Override
        public void onSuccess(Void aVoid) {
            Toast.makeText(MainActivity.this, "Image uri saved.", Toast.LENGTH_SHORT).show();
        }
    })
    .addOnFailureListener(new OnFailureListener() {
        @Override
        public void onFailure(@NonNull Exception e) {
            Toast.makeText(MainActivity.this, "Error occured.", Toast.LENGTH_SHORT).show();

        }
    });    
© www.soinside.com 2019 - 2024. All rights reserved.