0

I'm making an app for a school project which is basically a travel helper. The user can see random country cards at the home page so he can click and get more info about that country.

All my country card images are stored in the Firebase Storage in a directory called "countryCardImages". All the images have the name of the country that they represent so I can't retrieve that name using getName (a metadata function). The thing is, if I renamed all the photos from 1 to n, I could simply generate a random number and concatenate it to the link so everytime I opened the MainActivity, it would generate different numbers and show different images. The problem with this method is, I can't retrieve the country name because there is no place that holds that value. So, my only option is to name the images with their country name. Is there another way to display random images with this 'method'?

Database structure: enter image description here

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
André Silva
  • 37
  • 1
  • 16

1 Answers1

3

Yes it is. You can store all image urls in the Firebase Realtime database and when you want to retrieve a specific number of random images, use my answer from this post.

Assuming that you have a Firebase database that looks like this:

Firebase-root
   |
   --- imageUrls
          |
          --- HollandImageUrl: true
          |
          --- SpainImageUrl: true
          |
          --- FranceImageUrl: true
          |
          --- //Other Countries

To get 5 randomn urls, please use the following code:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference imageUrlsRef = rootRef.child("imageUrls");
ValueEventListener valueEventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        List<String> urlList = new ArrayList<>();
        for(DataSnapshot ds : dataSnapshot.getChildren()) {
            String url = ds.getKey();
            urlList.add(url);
        }

        int urlCount = urlList.size();
        int randomNumber = new Random().nextInt(urlCount);
        List<String> randomUrlList = new ArrayList<>();
        for (int i=1; i<=5; i++) {
            randomUrlList.add(urlList.get(randomNumber));
            //Set image to ImageView
        }
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {}
};
imageUrlsRef.addListenerForSingleValueEvent(valueEventListener);

According to your comment and your database structure, please use the following code:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference countriesRef = rootRef.child("countries");
ValueEventListener valueEventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        List<String> urlList = new ArrayList<>();
        for(DataSnapshot ds : dataSnapshot.getChildren()) {
            String url = ds.child("Image").getValue(String.class);
            urlList.add(url);
        }

        int urlCount = urlList.size();
        int randomNumber = new Random().nextInt(urlCount);
        List<String> randomUrlList = new ArrayList<>();
        for (int i=1; i<=5; i++) {
            randomUrlList.add(urlList.get(randomNumber));
            //Set image to ImageView
        }
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {}
};
countriesRef .addListenerForSingleValueEvent(valueEventListener);
Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • So with that code you gave I will be able to retrieve 5 images and display them in my ImageView? – André Silva May 07 '18 at 11:24
  • Thanks, @AlexMamo ! I'm not experienced with these NoSQL dabatases. Would this database format be good? [link](https://imgur.com/a/LRW3Q71)[/link] – André Silva May 07 '18 at 11:43
  • Yes and please see my updated answer that is according to your database structure. – Alex Mamo May 07 '18 at 11:55
  • Thanks, you're helping me a lot :) Inserting the image into my imageview wil be something like `Picasso.with(getContext()).load(_whatDoIPutHere_).into(imgCard3)` ? – André Silva May 07 '18 at 12:35
  • Picasso library is also good but i recommend you: [Glide for Android](https://github.com/bumptech/glide). – Alex Mamo May 07 '18 at 12:41
  • Hm, sweet. I have this `Glide.with(getContext()).load().into(imgCard3)` But what do I put in the `load()`? – André Silva May 07 '18 at 12:45
  • The `url` of your photo. Have you tried, does it work now? – Alex Mamo May 07 '18 at 12:48
  • One more thing... Whenever I try to Sync my gradle with Glide implementations, [this error](https://imgur.com/a/PgF44XC) appears... Do you have any ideia how to resolve this? – André Silva May 07 '18 at 13:00
  • You should update all dependencies to the last version. – Alex Mamo May 07 '18 at 13:08
  • That is strange, because that error only appears when I put the `implementation 'com.github.bumptech.glide:glide:4.7.1' annotationProcessor 'com.github.bumptech.glide:compiler:4.7.1'`. If I delete that, it becomes fine again. Yes, I will give you upvote. I just can't upvote more. I need to wait 12h. But I will upvote you thanks to your help :) – André Silva May 07 '18 at 13:16
  • When I use `Glide.with(getContext()).load(randomUrlList.get(i)).into(imgCard3);` the app crashes. Any idea why? And by the way, I already gave you the green check ;) – André Silva May 07 '18 at 13:46
  • There might be many reasons that can lead you to that error. Without seeing the entire context, I cannot say much. In order to follow the rules of this comunity, please post another fresh question, so me and other users can help you. – Alex Mamo May 07 '18 at 13:48
  • I figured it out ahah. The path that I need to insert into the DB is the "Download URL" or "Storage location"? – André Silva May 07 '18 at 14:11
  • Download URL. Good to hear that you figured out. Cheers! – Alex Mamo May 07 '18 at 14:23
  • Hm, one thing I've noticed. I have 5 Imageviews. How can I put different images in the three? If I use `Glide.with(getContext()).load(randomUrlList.get(i)).into(imgCard3)` than the three will have the same image! If this is another question, please, tell me that I will formulate it better and post it – André Silva May 07 '18 at 14:58
  • Yes, it is another question. Just create an [mcve](https://stackoverflow.com/help/mcve) and post the question. – Alex Mamo May 07 '18 at 14:59