【问题标题】:Postinf two images url to firebase database将两个图像 url 发布到 firebase 数据库
【发布时间】:2018-11-17 00:12:21
【问题描述】:

我正在努力将第二个 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();



    }


});

};

【问题讨论】:

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


    【解决方案1】:

    要记住的两件事:

    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();
    
    
    
                    }
    
    
    });
    
    
    
        }
    
    
    });
    

    【讨论】:

    • 谢谢,我刚做了Uri firebaseUri1;和 filepath1 Final,效果很好
    • 没有问题! @SifaksAgizul
    猜你喜欢
    • 2020-12-18
    • 1970-01-01
    • 1970-01-01
    • 2019-01-06
    • 1970-01-01
    • 2017-02-07
    • 2017-03-30
    • 1970-01-01
    • 2017-12-11
    相关资源
    最近更新 更多