用户不被存储在Firebase数据库,而图像存储在Firebase存储



我在Android Studio中使用Firebase身份验证的项目中工作。我不能在Firebase数据库中存储用户,而用户的图像被存储在Firebase存储。因此,它不能进行下一个活动,并且卡住了。

ActivitySetupProfileBinding binding;
FirebaseAuth auth;
FirebaseDatabase database;
FirebaseStorage storage;
Uri selectedImage;
ProgressDialog dialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivitySetupProfileBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
dialog = new ProgressDialog(this);
dialog.setMessage("Updating Profile..");
dialog.setCancelable(false);
database = FirebaseDatabase.getInstance();
storage = FirebaseStorage.getInstance();
auth = FirebaseAuth.getInstance();
getSupportActionBar().hide();
binding.imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent  intent = new Intent();
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
startActivityForResult(intent, 45);

}
});
binding.continueBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String name = binding.nameBox.getText().toString();
if(name.isEmpty()) {
binding.nameBox.setError("Name cannot be empty..");
return;
}
dialog.show();
if(selectedImage !=null) {
StorageReference reference = storage.getReference().child("Profiles").child(auth.getUid());
reference.putFile(selectedImage).addOnCompleteListener(new OnCompleteListener<UploadTask.TaskSnapshot>() {
@Override
public void onComplete(@NonNull Task<UploadTask.TaskSnapshot> task) {
if(task.isSuccessful()) {
reference.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
@Override
public void onSuccess(Uri uri) {
String imageUrl = uri.toString();
String uid = auth.getUid();
String phone = auth.getCurrentUser().getPhoneNumber();
String name = binding.nameBox.getText().toString();
User user = new User(uid, name, phone, imageUrl);
database.getReference()
.child("users")
.child(uid)
.setValue(user)
.addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
dialog.dismiss();
Intent intent = new Intent (SetupProfileActivity.this, MainActivity.class);
startActivity(intent);
finish();
}
});

}
});
}
}
});
} else {
String uid = auth.getUid();
String phone = auth.getCurrentUser().getPhoneNumber();
User user = new User(uid, name, phone, "No Image");
database.getReference()
.child("users")
.child(uid)
.setValue(user)
.addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
dialog.dismiss();
Intent intent = new Intent (SetupProfileActivity.this, MainActivity.class);
startActivity(intent);
finish();
}
});
}
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(data != null) {
if(data.getData() != null) {
binding.imageView.setImageURI(data.getData());
selectedImage = data.getData();
}
}
}

检查firebase控制台中访问存储的规则。默认情况下,所有经过身份验证的用户都可以读写存储。存储的默认规则是:

rules_version = '2';
service firebase.storage {
match /b/{bucket}/o {
match /{allPaths=**} {
allow read, write: if request.auth != null;
}
}
}

你应该在上传图像之前登录到firebase。

对于测试,您可以更改 行
allow read, write: if request.auth != null;

allow read write;

问题解决了。实际上,在Firebase数据库中,我将位置更改为欧洲,而不是默认位置(美国)。把地点改回美国就解决了。谢谢! !

相关内容

  • 没有找到相关文章

最新更新