[英]Download all files from firebase storage
I'm trying to download all images from my firebase storage. 我正在尝试从我的firebase存储中下载所有图像。 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). 我计划通过将每个图像的storageReference放到列表中然后遍历列表并创建downloadTasks集合(每个图像一个任务)来实现。 After that I wanted to use Tasks.whenAll() to download all at once. 之后我想使用Tasks.whenAll()一次下载所有内容。 The problem is that I can't add downloadTasks to the collection. 问题是我无法将downloadTasks添加到集合中。
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.");
}
});
}
}
Clearly it is because there is no data structure is defined to store the Task objects. 显然,这是因为没有定义数据结构来存储Task对象。 loadTask.add(task)
does not really do anything meaningful but return a boolean. loadTask.add(task)
并没有真正做任何有意义的事情但返回一个布尔值。 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. 在这里,您应该在适当的数据结构中定义Task对象的存储。 I really don't understand why you are not using a well defined collection already available in Java. 我真的不明白为什么你没有使用Java中已有的定义良好的集合。 Something like ArrayList , LinkedList etc. 像ArrayList , LinkedList等。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.