how to get URI of an image from Datasnapshot - android

I am making an app where I can upload images to firebase then retrieve them and view them in a viewpager.
The images have a name and an url.
In addition to that, I want to be able to write the name of the image in a textbox and display the corresponding image in an imageview.
This is the code I'm working with :
private void findImage(){
final Query query = mDatabaseRef.orderByChild("name").equalTo(EditTextName.getText().toString());
query.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
for (DataSnapshot NamedImage : dataSnapshot.getChildren()) {
//i have tried this first but i don't know what method i should use to get the URI from the datasnapshot.
image.setImageURI(NamedImage.getValue());
//then i have tried this but i can't use "context" here, nor can i apply .getImageUrl to the datasnapshot.
Glide.with(context).load(NamedImage.getImageUrl()).into(image);
}
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
I am new to android studio and java and I haven't studied it before starting this application.
Any idea on what I should do please?

I found the solution to my problem :
private void findImage(){
final Query query = mDatabaseRef.orderByChild("name").equalTo(EditTextName.getText().toString());
query.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
for (DataSnapshot NamedImage : dataSnapshot.getChildren()) {
SliderUtils sliderUtils = NamedImage.getValue(SliderUtils.class);
Glide.with(getApplicationContext()).load(sliderUtils.getImageUrl()).into(image);
}
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}

Related

Jump to Firebase parent Nodes from Child Nodes

Hello all i have a my fire-base structure like this
Firebase Database
I have an array list inside an node in fire-base , I need to loop through this list and jump to each ID to get information i need like the database photo shows.
what I've tried so far
public static void checkIfCurrentUserLiked(final HomeFragment.PostsViewHolder postsViewHolder, String postKey) {
userID = Objects.requireNonNull(FirebaseAuth.getInstance().getCurrentUser()).getUid();
likesRef = FirebaseDatabase.getInstance().getReference().child("likes");
postsRef = FirebaseDatabase.getInstance().getReference().child(postKey);
postsRef.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
if (dataSnapshot.hasChild("likes")) {
for (DataSnapshot ds : dataSnapshot.child("likes").getChildren()) {
likesRef.child(Objects.requireNonNull(ds.getValue()).toString())
.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot snapshot) {
Like like = snapshot.getValue(Like.class);
if (like != null && like.getLikedUserID().equals(userID)) {
postsViewHolder.likesImage.setImageResource
(R.drawable.ic_item_home_post_like_filled);
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) { }
});
}
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) { }
});
}
i have no error , just nothing happens , data are not fetched as expected
Change this:
postsRef = FirebaseDatabase.getInstance().getReference().child(postKey);
into this:
postsRef = FirebaseDatabase.getInstance().getReference().child("posts").child(postKey);
According to your database, you have node posts before the postKey, therefore you need to add it when referencing to a location.

Retrieving Data from Firebase with certain ID

I have an ID of User, and i want to take his name from the firebase. So, i am trying to use orderByKey() method to find a certain user and after that, take information from his profile. But something goes wrong ...
reference = FirebaseDatabase.getInstance().getReference("Users");
Query query = reference.orderByKey().equalTo(task.author_id);
query.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
fullName = dataSnapshot.getValue(User.class).getFullName();
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
Firebase Structure
You don't need a query in that case if you already know the specific path to that node. Just add the listener directly.
reference = FirebaseDatabase.getInstance().getReference("Users");
reference.child(task.author_id).addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
fullName = dataSnapshot.getValue(User.class).getFullName();
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});

Unit testing Firebase

I am trying to write unit tests for methods that load and add data from Firebase DB. I have not found any resources to help me with this. can someone point me to a resource or post a sample code? for example, this is my load method
public ArrayList getGroceryData(final String type) {
tempBuilder=new ArrayList<>();
test=false;
databaseReference.child("Tasks").child(type).addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
for (DataSnapshot d : dataSnapshot.getChildren()) {
tempBuilder.add(d.getValue(Task.class));
}
test=true;
// Dashboard.populatefeed(tempBuilder);
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
while(!test);
test=false;
return tempBuilder;
}

How to use multiple orederbychild in Firebase database?

Hello Everyone I am using firebase database in my app. I am able to add data to database. Now I want to implement search in my app, I have two option for search users,
1)Blood Group
2)User Area
I am able to get data as per blood group selection, but I don't know how can I fetch data with blood group and area. (Multiple Selection Filter)
Now if the user will select 'A+' as blood group from spinner and select Area 'ABC'
Then result would come as Users with 'A+' blood group and 'ABC' area.
search_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
System.out.println("Default Selected"+sel_blood_group);
mFirebaseDatabase.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
//Your Logic here
for (DataSnapshot eventSnapshot : dataSnapshot.getChildren()) {
UserRegisterModel mModel = eventSnapshot.getValue(UserRegisterModel.class);
// Log.e("DATA" ,""+ mModel.getName());
}
Query chatRoomsQuery = mFirebaseDatabase.orderByChild("blood_group").equalTo(sel_blood_group);
chatRoomsQuery.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
// dataSnapshot is the "issue" node with all children with id 0
for (DataSnapshot issue : dataSnapshot.getChildren()) {
// do something with the individual "issues"
UserRegisterModel mModel = issue.getValue(UserRegisterModel.class);
Log.e("QUERY DATA" ,""+ mModel.getName());
}
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
Log.e("DATA" ,""+ chatRoomsQuery.toString());
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
});
Firebase RealTime Database does not support multiple where clauses. So try to query with one filter and then filter the next one programatically.
search_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
System.out.println("Default Selected"+sel_blood_group);
System.out.println("Default Selected"+sel_area);
mFirebaseDatabase.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
//Your Logic here
for (DataSnapshot eventSnapshot : dataSnapshot.getChildren()) {
UserRegisterModel mModel = eventSnapshot.getValue(UserRegisterModel.class);
// Log.e("DATA" ,""+ mModel.getName());
}
Query chatRoomsQuery = mFirebaseDatabase.orderByChild("blood_group").equalTo(sel_blood_group);
chatRoomsQuery.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
// dataSnapshot is the "issue" node with all children with id 0
for (DataSnapshot issue : dataSnapshot.getChildren()) {
// do something with the individual "issues"
UserRegisterModel mModel = issue.getValue(UserRegisterModel.class);
if(mModel.getArea().equals(sel_area))
Log.e("QUERY DATA" ,""+ mModel.getName());
}
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
Log.e("DATA" ,""+ chatRoomsQuery.toString());
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
});enter code here
You can make userName to be added only once using the following method. I have assumed that you are storing the user details in UserRegisterModel class. Please take care of the DatabaseReferences as I have used what you have mentioned and I am not aware of your Database structure.
Query chatRoomsQuery = mFirebaseDatabase.orderByChild("User_Name").equalTo(userRegisterModel.getUserName());
chatRoomsQuery.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
Log.e("UserAlready Registered");
}
else
{
mFirebaseDatabase.push().setValue(userRegisterModel);;
Log.i("User added Successfully");
}
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
I have not tested the code but I think it will work! :)

How can I get some value from my Firebase?

I am trying to solve how to get a value from my DatabaseReference, but doing so gives me the full address when I want to put it in a TextView
This is the reference:
public static DatabaseReference getUsernameRef(String email){
return FirebaseDatabase.getInstance().getReference("nom_usuarios");
These are the values that I want to rescue:
i tried this:
mPosti.setUsername(FirebaseUtils.getUsernameRef(FirebaseUtils.getCurrentUser().getEmail()).toString());
But instead of receiving the value, I get the address of the database.
DatabaseReference database = FirebaseDatabase.getInstance().getReference().child("nom_usuaios").getRef();
database.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
// here you will the value in datasnapshot
for (DataSnapshot dataSnapshot : dataSnapshot.getChildren()) {
list.add(dataSnapshot.getValue());
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});

Categories

Resources