简体   繁体   中英

Get random image from Firebase Storage

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: 在此处输入图片说明

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM