[英]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.