簡體   English   中英

關於 firebase 存儲和 firebase 防火牆工作,我無法通過帶有 setValue 的片段在 Firebase 數據庫中添加數據

[英]I cant add data in Firebase Database through a Fragment with setValue, regarding firebase storage and firebase firestore working

我的“AddFragment.java”文件中有以下代碼,用於將數據添加到 firebase 防火牆、firebase 存儲和 firebase 數據庫中。 關於 firebase 存儲和 firebase firestore 工作,firebase 數據庫不工作,盡管配置正確。

imageUpload.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {

        if(filePath!=null) {

            String LostPetName = petName.getText().toString().trim();
            String userID = firebaseAuth.getCurrentUser().getUid();
            String path = userID+"_"+filePath.hashCode()+".jpg";
            StorageReference petPhotos = storageReference.child(path);
            DocumentReference documentReference = firebaseFirestore.collection("Lost_Pets").document(path);
            DatabaseReference myDatabase = firebaseDatabase.getReference("pets");

            //                      -------------------------

            //                      ADDING AN IMAGE START

            petPhotos.putFile(filePath).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                @Override
                public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                    Toast.makeText(getActivity(), "Imagine încărcată cu succes", Toast.LENGTH_SHORT).show();

                    //                      -------------------------

                    //                      ADDING A MAP LatLng START

                    String markerId = myDatabase.push().getKey();
                    AddMarker coord = new AddMarker(lastKnownLocation.getLatitude(), lastKnownLocation.getLongitude(), userID);
                    assert markerId != null;
                    myDatabase.child(markerId).setValue(coord);

                    //                      ADDING A MAP LatLng END

                    //                      -------------------------

                    petPhotos.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>()
                    {
                        @Override
                        public void onSuccess(Uri downloadUrl)
                        {
                            imgUri = downloadUrl.toString();
                            imgID = imgUri;
                            Map<String,Object> pet = new HashMap<>();
                            pet.put("LostPetName", LostPetName);
                            pet.put("img_id", imgID);
                            pet.put("markerLatitude", String.valueOf(lastKnownLocation.getLatitude()));
                            pet.put("markerLongitude", String.valueOf(lastKnownLocation.getLongitude()));
                            documentReference.set(pet);

                        }
                    });

                }
            })
                    .addOnFailureListener(new OnFailureListener() {
                        @Override
                        public void onFailure(@NonNull Exception e) {
                            Toast.makeText(getActivity(), "Eșuare adăugare imagine", Toast.LENGTH_SHORT).show();
                        }
                    });

            //                      ADDING AN IMAGE END

            //                      -------------------------

        }
        else{
            Toast.makeText(getActivity(),"Nu s-a ales o imagine pentru animalul pierdut", Toast.LENGTH_SHORT).show();
        }
    }
});
}

如果我在setValue function 之后添加Log.d(TAG, String.valueOf(lastKnownLocation.getLatitude()) + String.valueOf(lastKnownLocation.getLongitude()) + userID) ,我可以在日志中正確看到我的數據。

AddMarker function 如下。

public static class AddMarker {

    public double lat;
    public double lng;
    public String senderUid;

    public AddMarker(double lat, double lng, String senderUid) {
        this.lat = lat;
        this.lng = lng;
        this.senderUid = senderUid;
    }
}

我在 gradle 文件中正確配置了 firebase。

帳戶連接和實施集:

implementation 'com.google.firebase:firebase-database:20.0.0'

以及 Firebase 中的規則:

{
  "rules": {
    ".read": true,
    ".write": true
  }
}

如果有人知道我不知道的事情,請幫助我。 我將不勝感激。

從評論鏈看來,您可能在創建實時數據庫之前下載了google-services.json ,這可能導致它使用了錯誤的 URL。

我建議下載更新的google-services.json ,將添加到您的 Android 應用程序中,然后重試。 如果這不起作用,您還可以使用FirebaseDatabase.getInstance("rescue-a-soul-default-rtdb.europe-west1.firebasedatabase.app")...在代碼中指定正確的 URL。

另見:

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM