更新Firebase实时数据库的价值的问题

时间:2018-08-11 11:52:46

标签: android firebase firebase-realtime-database

我试图在Firebase实时数据库上推送一些数据,但是我面临的问题是,上传图像后,它没有更新图像属性值(图像的默认值为defaultimg )。无需更新值,而是向新节点添加了image的值。

下面是代码。

private void postAd(final String title,final String description,final String category,final
        String price) {
    FirebaseUser currentUser = FirebaseAuth.getInstance().getCurrentUser();
    String uid = currentUser.getUid();

    mDatabase = FirebaseDatabase.getInstance().getReference().child("AdvertData");
    HashMap<String,String> userMap = new HashMap<>();
    userMap.put("title", title);
    userMap.put("description",description);
    userMap.put("category",category);
    userMap.put("image","defaultimg");
    userMap.put("price",price);

    mDatabase.push().setValue(userMap).addOnCompleteListener(new OnCompleteListener<Void>() {
        @Override
        public void onComplete(@NonNull Task<Void> task) {
            if(task.isSuccessful()){
                mProgess.hide();
                Toast.makeText(getView().getContext(),"Data Uploaded Successfully",Toast
                        .LENGTH_SHORT).show();
                mTitle.getEditText().setText(null);
                mDescription.getEditText().setText(null);
                mPrice.getEditText().setText(null);
                mCategory.setText("Category");
            }else{
                Toast.makeText(getView().getContext(),"Data Uploaded Unuccessfully",Toast
                        .LENGTH_SHORT).show();
            }

上面的代码部分用于将数据上传到数据库

以下内容将用于onActivityResult方法来更新子级中的图像。

mImageStorage.child("ad_images").child(current_user_id+".jpg")
  .getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
    @Override
    public void onSuccess(Uri uri) {
        String downloadUrl = uri.toString();
        mUserRef = FirebaseDatabase.getInstance().getReference();
        DatabaseReference adRef = mUserRef.child("AdvertData");
        String key = adRef.push().getKey();

        if (key != null) {
            adRef.child(key).child("image").setValue(downloadUrl)
              .addOnCompleteListener(new OnCompleteListener<Void>() {
                @Override
                public void onComplete(@NonNull Task<Void> task) {
                    if (task.isSuccessful()){
                        mProgess.dismiss();
                        Toast.makeText(getActivity(), "Ad picture uploaded completely.", Toast.LENGTH_SHORT).show();
                    }
                }
            });
        }

1 个答案:

答案 0 :(得分:0)

好的,所以在应用程序上工作了6个小时之后,我找到了一个解决方案,而不是从firebase获取随机生成的密钥来更新图像的值。每当用户数据上传到Firebase时,我就上传了图像。因此,简而言之,我没有在相机活动的OnActivityResult中更新图像,而是在将数据上传到实时数据库时上传了图像。