1 year ago
#72274
Sindano
How to reference realtime database in Firebase from Android Studio
I'm creating an app where people can see how their progress is on a course. So I'm trying to upload a progress bar image onto real time database in Firebase, I do this by uploading the image in Firebase Storage then copying the link and pasting it into a directory under each user's id. In Android Studio I'm finding it hard to reference the specific folder.
The Code:
ImageView imageView, imageView2;
private FirebaseDatabase firebaseDatabase = FirebaseDatabase.getInstance();
private DatabaseReference databaseReference = firebaseDatabase.getReference();
FirebaseAuth fAuth;
private DatabaseReference first = databaseReference.child("image");
private DatabaseReference second = databaseReference.child("image2");
// user name
TextView greeting;
private String userID;
private DatabaseReference reference;
private FirebaseUser user;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_12);
firebaseDatabase = FirebaseDatabase.getInstance();
databaseReference = firebaseDatabase.getReference("Data");
user = FirebaseAuth.getInstance().getCurrentUser();
reference = FirebaseDatabase.getInstance().getReference("Users");
userID = user.getUid();
imageView = findViewById(R.id.imageView);
imageView2 = findViewById(R.id.imageView2);
lLayout1 = findViewById(R.id.lLayout1);
lLayout2 = findViewById(R.id.lLayout2);
lLayout3 = findViewById(R.id.lLayout3);
lLayout4 = findViewById(R.id.lLayout4);
lLayout5 = findViewById(R.id.lLayout5);
lLayout6 = findViewById(R.id.lLayout6);
gridView = findViewById(R.id.gridView);
fab = (FloatingActionButton) findViewById(R.id.fab);
//animations
fabOpen = AnimationUtils.loadAnimation(this, R.anim.fab_open);
fabClose = AnimationUtils.loadAnimation(this, R.anim.fab_close);
rotateForward = AnimationUtils.loadAnimation(this, R.anim.rotate_forward);
rotateBackwards = AnimationUtils.loadAnimation(this, R.anim.rotate_backwards);
//Initialize and assign variable
BottomNavigationView bottomNavigationView = findViewById(R.id.bottom_navigation);
//Set Home selected
bottomNavigationView.setSelectedItemId(R.id.home);
//Perform ItemSelectorListener
bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
switch (menuItem.getItemId()) {
case R.id.home2:
return true;
case R.id.not2:
startActivity(new Intent(getApplicationContext()
, Activity3.class));
overridePendingTransition(0, 0);
return true;
}
return false;
}
});
final TextView greetingTextView = findViewById(R.id.greeting);
final TextView fullNameTextView = findViewById(R.id.fullName);
reference.child(userID).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
User userProfile = snapshot.getValue(User.class);
if (userProfile != null) {
String fullName = userProfile.fullName;
greetingTextView.setText(fullName);
}
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
Toast.makeText(Activity12.this, "Something wrong happend!", Toast.LENGTH_LONG).show();
}
});
}
@Override
protected void onStart() {
super.onStart();
first.child(userID).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange( DataSnapshot dataSnapshot) {
String link = dataSnapshot.getValue(String.class);
Picasso.get().load(link).into(imageView);
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
second.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange( DataSnapshot dataSnapshot) {
String link = dataSnapshot.getValue(String.class);
Picasso.get().load(link).into(imageView2);
}
My Realtime database in Firebase
{
"Users" : {
"EzdWQOQ9W0UH5pscGF8nqviynMZ2" {:
"email" : "sindanoo@outlook.com",
"fullName" : "Eric",
"image" : "https://firebasestorage.googleapis.com/v0/b/rent-to-own-93ff1.appspot.com/o/users%2Fuser_progress%2FIncomplete%20Bar.png?alt=media&token=638e1d13-7ce5-405e-b36d-6a1e46af97d4"
},
"UcbGBa0jlXhnMhT0UNPa2fT4nDH2" : {
"email" : "sinddano@hotmail.com",
"fullName" : "Sindano"
},
"y4MYqKRT3mYu4wokyQrhScIyasN2" : {
"email" : "sindogg90@gmail.com",
"fullName" : "Isaac",
"image" : "https://firebasestorage.googleapis.com/v0/b/rent-to-own-93ff1.appspot.com/o/users%2Fuser_progress%2FIncomplete%20Bar.png?alt=media&token=638e1d13-7ce5-405e-b36d-6a1e46af97d4"
}
My User Class
public class User {
public String fullName, age, email, dateJoined, property, installment, homeValue;
public User() {
}
public User(String fullName, String age, String email, String dateJoined, String property, String installment,String homeValue) {
this.fullName = fullName;
this.age = age;
this.email = email;
this.dateJoined = dateJoined;
this.property = property;
this.installment = installment;
this.homeValue = homeValue;
}
android
firebase-realtime-database
firebase-authentication
imageview
firebase-storage
0 Answers
Your Answer