Reputation: 83
I actually I'm trying to upload an image to Firestorage then add URL to Firebase database. my code is run fine and successfully upload the selected image to storage under specific userId my problem is when adding data to database it's executed successfully as shown in logcat but when I check in database added data is not appear I checked the permission as well security role of Firebase it's set right but still add data to nodes not appear is any error in my code or what?
private void UpoloadImageTofirebaseStorage() {
user_id = mAuth.getCurrentUser().getUid();
mStorageRef = FirebaseStorage.getInstance().getReference("users").child(user_id);
if (imagePath != null) {
// final StorageReference imageRef = mStorageRef.child("users").child(user_id);
UploadTask uploadTask = mStorageRef.putFile(imagePath);
uploadTask.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.i(TAG, "Store image in storage:failure", e);
}
});
uploadTask.addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
Task<Uri> downloadUrl = mStorageRef.getDownloadUrl();
Log.i(TAG,"Image Added "+ downloadUrl);
downloadUrl.addOnSuccessListener(new OnSuccessListener<Uri>() {
@Override
public void onSuccess(Uri uri) {
profileImageURL = uri.toString();
Log.i(TAG,"File Location"+ profileImageURL);
SaveUserToFirebaseDatabase( profileImageURL);
}
});
}
});
}
}
// add user info to datadase
private void SaveUserToFirebaseDatabase(String profileImageURL)
{
user_id = mAuth.getCurrentUser().getUid();
CreationDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault()).format(new Date());
MyUsers user = new MyUsers(FullName,Password,EmailAddress,Gender, profileImageURL,CreationDate);
//add user to profile & all
DatabaseReference mDatabase= FirebaseDatabase.getInstance().getReference("users/profile").child(user_id);
mDatabase.setValue(user).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.i(TAG," mDatabase unable to add data"+ e);
}
});
Toast.makeText( UserSignup.this, "your information Added Successfully ", Toast.LENGTH_LONG).show();
}
all added data appear in logcat but still not appear as node in firebase
Upvotes: 1
Views: 78
Reputation: 2401
You could be missing the following push()
method. Try this:
mDatabase.push().setValue(user)
Upvotes: 2