0

My code to display images in an image slider(slideshow) is working when I retrieve images from Firestore, HOWEVER I am trying to retrieve it from Firebase storage instead and it's giving me a hard time.

This is my working code for retrieving from Firestore.

public void update(View view) {
        DocumentReference user=db.collection("FILES").document("images");
        user.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
            @Override
            public void onComplete(@NonNull Task<DocumentSnapshot> task) {
                if (task.isSuccessful())
                {
                    DocumentSnapshot doc=task.getResult();
                    //For Image1
                    StringBuilder img1=new StringBuilder("");
                    img1.append(doc.get("image1"));
                    image1.setText(img1.toString());
                    String image1url=image1.getText().toString();
                    Picasso.get().load(image1url).into(imageView1);

                    //For Image2
                    StringBuilder img2=new StringBuilder("");
                    img2.append(doc.get("image2"));
                    image2.setText(img2.toString());
                    Stringimage2url=image2.getText().toString();
                       Picasso.get().load(image2url).into(imageView2);
}
            }
        }).addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception e) {
                Toast.makeText(ImageSlider.this, "Falied", Toast.LENGTH_SHORT).show();
            }
        });
    }

I tried this to get it from firebase storage but 'get()' is showing up as an error in

get().addOnCompleteListener(new OnCompleteListener

Any ideas?

get() is not working when I try the following:

public void update(View view) {
        DatabaseReference mDatabaseRef = FirebaseDatabase.getInstance().getReference().child("uploads").child("uploads");
        mDatabaseRef.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
            @Override
            public void onComplete(@NonNull Task<DocumentSnapshot> task) {
CoderGirl94
  • 214
  • 2
  • 12
  • And the error is...? – Doug Stevenson Feb 25 '19 at 20:44
  • Your are not even using the Firebase Storage API! 1. Your are creating a DatabaseReference instead of StorageReference. 2. There is no get() method in StorageReference (nor in DatabaseReference). https://firebase.google.com/docs/storage/android/download-files – 3li Feb 25 '19 at 20:48
  • @3li I want to access Real-time database. FirebaseStorage seems to update real-time db with the same image files. So if you know what alternative I could use instead of get(), that would be helpful :) – CoderGirl94 Feb 25 '19 at 21:05
  • @CoderGirl94 You titled the question "How can I retrieve images from Firebase Storage", so I thought you wanted to use Firebase Storage. Anyway, you need to add a ValueEventListener to the DatabaseReference. Use the method addValueEventListener on the DatabaseReference object to read and listen for the changes to the value, or use addListenerForSingleValueEvent to read the value only once. Read the documentation for more https://firebase.google.com/docs/database/android/read-and-write – 3li Feb 25 '19 at 21:23

1 Answers1

0
private void getData () {             
        DatabaseReference reference;
        reference.child("whatever").addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                Model model = dataSnapshot.getValue(Model.class);
                imageURL = model.getUrl();


            }
            @Override
            public void onCancelled(DatabaseError databaseError) {
            }
        });
    }

Change addListenerForSingleValueEventif you need to read value more than once and values with your values..