从存储中获取Url后,如何将其发布到数据库中?

时间:2018-12-29 08:55:56

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

Storage getDownloadUrl()方法已经过时,我遵循了新方法,但由于未传递到firebaseUri中存储的url,因此无法将链接发布到数据库中。链接将被存储,但其他字段将不会存储在数据库中。因此,我几乎陷入了困境。

我遵循该方法来获取URL链接及其工作,但是因为我必须将其他字段和图像URL链接一起发布,所以我似乎无法这样做

private void upload() {
    if (selectedImageUri != null) {

        final String postId = FirebaseDatabase.getInstance().getReference().push().getKey();

        final StorageReference ref = storageReference.child("posts/users/"+ FirebaseAuth.getInstance().getCurrentUser().getUid() + "/post_image" );

        ref.putFile(selectedImageUri)
                .addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                    @Override
                    public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                        progressDialog.dismiss();
                        Toast.makeText(PostItemActivity.this, "Uploaded", Toast.LENGTH_SHORT).show();



                        ref.putFile(selectedImageUri).continueWithTask(new Continuation<UploadTask.TaskSnapshot, Task<Uri>>() {
                            @Override
                            public Task<Uri> then(@NonNull Task<UploadTask.TaskSnapshot> task) throws Exception {
                                if (!task.isSuccessful()) {
                                    throw task.getException();

                                }

                                return ref.getDownloadUrl();
                            }
                        }).addOnCompleteListener(new OnCompleteListener<Uri>() {
                            @Override
                            public void onComplete(@NonNull Task<Uri> task) {
                                if (task.isSuccessful()) {
                                    Uri firebaseUri = task.getResult().toString();

                                }

                            }
                        });

                            Post post = new Post();
                            DatabaseReference reference = FirebaseDatabase.getInstance().getReference();
                            post.setImage(firebaseUri.toString());
                            post.setTitle(InputTitle.getText().toString());
                            post.setDescription(InputItemDescription.getText().toString());
                            post.setPost_id(postId);
                            reference.child(getString(R.string.node_posts))
                                    .child(postId)
                                    .setValue(post);


                    }
                })


    }
}

0 个答案:

没有答案