Download all files from firebase storage

Openzz :

I'm trying to download all images from my firebase storage. I planned to do that by putting storageReference of every image to a list and then go through the list and create a collection of downloadTasks (one task for every image). After that I wanted to use Tasks.whenAll() to download all at once. The problem is that I can't add downloadTasks to the collection.

My testing code looks like this:

package com.example.testdownload;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.widget.ImageView;

import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.android.gms.tasks.Task;
import com.google.android.gms.tasks.Tasks;
import com.google.firebase.storage.FileDownloadTask;
import com.google.firebase.storage.FirebaseStorage;
import com.google.firebase.storage.ListResult;
import com.google.firebase.storage.StorageReference;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

public class MainActivity extends AppCompatActivity {


    ImageView imageView;

    ArrayList<StorageReference> firebaseImagesUrls;
    ArrayList<Bitmap> images;

    Collection<Task<FileDownloadTask>> loadTasks;

    Bitmap bitmap;

    FirebaseStorage firebaseStorage = FirebaseStorage.getInstance();
    StorageReference storageReference = firebaseStorage.getReferenceFromUrl("gs://ehotel-3a76c.appspot.com");

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        imageView = (ImageView)findViewById(R.id.imageView);

        images = new ArrayList<>();
        firebaseImagesUrls = new ArrayList<>();

        loadTasks = new Collection<Task<FileDownloadTask>>() {
            @Override
            public int size() {
                return 0;
            }

            @Override
            public boolean isEmpty() {
                return false;
            }

            @Override
            public boolean contains(@Nullable Object o) {
                return false;
            }

            @NonNull
            @Override
            public Iterator<Task<FileDownloadTask>> iterator() {
                return null;
            }

            @NonNull
            @Override
            public Object[] toArray() {
                return new Object[0];
            }

            @NonNull
            @Override
            public <T> T[] toArray(@NonNull T[] ts) {
                return null;
            }

            @Override
            public boolean add(Task<FileDownloadTask> fileDownloadTaskTask) {
                return false;
            }

            @Override
            public boolean remove(@Nullable Object o) {
                return false;
            }

            @Override
            public boolean containsAll(@NonNull Collection<?> collection) {
                return false;
            }

            @Override
            public boolean addAll(@NonNull Collection<? extends Task<FileDownloadTask>> collection) {
                return false;
            }

            @Override
            public boolean removeAll(@NonNull Collection<?> collection) {
                return false;
            }

            @Override
            public boolean retainAll(@NonNull Collection<?> collection) {
                return false;
            }

            @Override
            public void clear() {

            }
        };

        storageReference.listAll().addOnSuccessListener(new OnSuccessListener<ListResult>() {
            @Override
            public void onSuccess(ListResult listResult) {
                for (StorageReference item : listResult.getItems()){
                    //Add code to save images here
                    firebaseImagesUrls.add(item);
                }

                for(StorageReference imgRef : firebaseImagesUrls)
                {
                    try{
                        final File theImage = File.createTempFile("img", "png");

                        Task task = imgRef.getFile(theImage);
                        task.addOnSuccessListener(new OnSuccessListener() {
                            @Override
                            public void onSuccess(Object o) {
                                bitmap = BitmapFactory.decodeFile(theImage.getAbsolutePath());
                                images.add(bitmap);
                            }
                        });
                        //THE PROBLEM IS RIGHT UNDER: loadTasks is always empty.
                        loadTasks.add(task);
                    }
                    catch(IOException e)
                    {
                        e.printStackTrace();
                    }

                }
                Task allLoads = Tasks.whenAll(loadTasks);
                allLoads.addOnSuccessListener(new OnSuccessListener() {
                    @Override
                    public void onSuccess(Object o) {
                        //Do smth with loaded images
                    }
                });
            }
        }).addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception e) {
                System.out.println("ListAll() failed.");
            }
        });
    }
}


Anjana :

Clearly it is because there is no data structure is defined to store the Task objects. loadTask.add(task) does not really do anything meaningful but return a boolean. Therefore this is just a generic class without a real implementation.

@Override
public boolean add(Task<FileDownloadTask> fileDownloadTaskTask) {
   return false;
}

Here, you should define the storing of Task object in a suitable data structure. I really don't understand why you are not using a well defined collection already available in Java. Something like ArrayList, LinkedList etc.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=126976&siteId=1