Postinf两个图像URL到firebase数据库

时间:2018-11-17 00:12:22

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

我正在努力将第二个URL添加到数据库中。我尝试了其他几种方法,但并没有走得太远。我曾尝试分别上传每个文件,但后来创建了两个孩子。我最终遵循了Upload Multiple Images on Firebase - Android Studio的回答,就在这里。

这是我要实现的目标:

 posts
      -LRTx-3WJNP2zxifv7h9
         city:
         contact_email:
         country:
         description:
         image:  URL IMAGE IN STORAGE
         image1:  URL IMAGE1 IN STORAGE
         post_id:
         price:
         state_province:
         title:
         user_id: 

两个图像均已成功上传到存储,但我在数据库中仅得到一个网址,如下所示: image1不存在。

posts
      -LRTx-3WJNP2zxifv7h9
         city:
         contact_email:
         country:
         description:
         image:  URL IMAGE IN STORAGE
         post_id:
         price:
         state_province:
         title:
         user_id: 

代码:

if( mSelectedUri !=null ){

Toast.makeText(getActivity(), "uploading image", Toast.LENGTH_SHORT).show();

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

StorageReference filepath = FirebaseStorage.getInstance().getReference()
        .child("posts/users/" + FirebaseAuth.getInstance().getCurrentUser().getUid()+
                "/" + postId + "/post_image").child(mSelectedUri.getLastPathSegment());

StorageReference filepath1 = FirebaseStorage.getInstance().getReference()
        .child("posts/users/" + FirebaseAuth.getInstance().getCurrentUser().getUid() +
                "/" + postId + "/post_image1").child(mSelectedUri1.getLastPathSegment());




filepath.putFile(mSelectedUri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {

    public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {


        Uri firebaseUri = taskSnapshot.getDownloadUrl();

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

        Post post = new Post();
        post.setImage(firebaseUri.toString());
      //  post.setImage1(firebaseUri.toString());

        post.setCity(mCity.getText().toString());
        post.setContact_email(mContactEmail.getText().toString());
        post.setCountry(mCountry.getText().toString());
        post.setDescription(mDescription.getText().toString());
        post.setPost_id(postId);
        post.setPrice(mPrice.getText().toString());
        post.setState_province(mStateProvince.getText().toString());
        post.setTitle(mTitle.getText().toString());
        post.setUser_id(FirebaseAuth.getInstance().getCurrentUser().getUid());

        reference.child(getString(R.string.node_posts))
                .child(postId)
                .setValue(post);



    }


});




filepath1.putFile(mSelectedUri1).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {

    public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {


        Uri firebaseUri = taskSnapshot.getDownloadUrl();

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

        Post post = new Post();
        post.setImage1(firebaseUri.toString());

        reference.child(getString(R.string.node_posts))
                .child(postId)
                .setValue(post);

       // resetFields();



    }


});

};

1 个答案:

答案 0 :(得分:1)

2件事要记住:

1)Firebase监听器本质上不是同步的。可能不会遵循您编写侦听器的顺序。

2)在Firebase中的某个位置书写时,它将用新对象替换以前的整个对象。

现在,您的filepath1 onSuccessListener首先执行。之后,将执行文件路径onSuccessListener,该操作将覆盖文件路径1 onSuccessListener在数据库中写入的内容。这就是为什么您得到此结果。您可以在执行此命令时通过查看数据库来检查自己。

尝试此代码。它应该可以解决您的问题。

if( mSelectedUri !=null ){

Toast.makeText(getActivity(), "uploading image", Toast.LENGTH_SHORT).show();

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

StorageReference filepath = FirebaseStorage.getInstance().getReference()
        .child("posts/users/" + FirebaseAuth.getInstance().getCurrentUser().getUid()+
                "/" + postId + "/post_image").child(mSelectedUri.getLastPathSegment());

StorageReference filepath1 = FirebaseStorage.getInstance().getReference()
        .child("posts/users/" + FirebaseAuth.getInstance().getCurrentUser().getUid() +
                "/" + postId + "/post_image1").child(mSelectedUri1.getLastPathSegment());
        Uri firebaseUri1;




filepath.putFile(mSelectedUri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {

public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {


    firebaseUri1 = taskSnapshot.getDownloadUrl();

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






            filepath1.putFile(mSelectedUri1).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {

            public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {


                    Uri firebaseUri2 = taskSnapshot.getDownloadUrl();

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

                        Post post = new Post();
                        post.setImage(firebaseUri1.toString());
                      //  post.setImage1(firebaseUri.toString());

                        post.setCity(mCity.getText().toString());
                        post.setContact_email(mContactEmail.getText().toString());
                        post.setCountry(mCountry.getText().toString());
                        post.setDescription(mDescription.getText().toString());
                        post.setPost_id(postId);
                        post.setPrice(mPrice.getText().toString());
                        post.setState_province(mStateProvince.getText().toString());
                        post.setTitle(mTitle.getText().toString());
                        post.setUser_id(FirebaseAuth.getInstance().getCurrentUser().getUid());
                        post.setImage1(firebaseUri2.toString());


                        reference.child(getString(R.string.node_posts))
                                .child(postId)
                                .setValue(post);



   // resetFields();



                }


});



    }


});