[英]Android : download files list from Firebase storage
I uploaded a file list to Firebase that I want to download using my app. 我将文件列表上传到Firebase,我想使用我的应用程序下载该文件。
List<String> childsRef = new ArrayList<>();
childsRef.add("xxxx/img1");
childsRef.add("xxxx/img2");
... etc
Then, through this list, I try to download files using my Firebase storageReference : 然后,通过该列表,我尝试使用Firebase storageReference下载文件:
for (String child : childsRef) {
islandRef = storageRef.child(child);
File localFile = File.createTempFile("images", "jpg");
islandRef.getFile(localFile).addOnSuccessListener(new OnSuccessListener<FileDownloadTask.TaskSnapshot>() {
@Override
public void onSuccess(FileDownloadTask.TaskSnapshot taskSnapshot) {
// Local temp file has been created
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception exception) {
// Handle any errors
}
});
}
The download process is async, so I can't show pop-ups to visualize downloading progression... I want to navigate to next Activity only if all of pending downloads are done.. 下载过程是异步的,因此我无法显示弹出窗口来直观显示下载进度...仅在所有未完成的下载完成后,我才想导航到下一个活动。
Do you have any ideas/help? 您有什么想法/帮助吗?
--EDIT Solution : -编辑解决方案:
FirebaseStorage instance = FirebaseStorage.getInstance();
StorageReference referenceFromUrl = instance.getReferenceFromUrl("gs://xxxxxxx.appspot.com/");
for (final String aur : aurl) {
final File localFile = new File(PATH + aur.substring(aur.lastIndexOf("/") + 1, aur.lastIndexOf(".")) + ".dat");
StorageReference f = referenceFromUrl.child(aur);
FileDownloadTask task = f.getFile(localFile);
task.addOnSuccessListener(new OnSuccessListener<FileDownloadTask.TaskSnapshot>() {
@Override
public void onSuccess(FileDownloadTask.TaskSnapshot taskSnapshot) {
size += localFile.length();
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception exception) {
Log.e("firebase ", ";local tem file not created created " + exception.toString());
}
});
while (!task.isComplete()) {
}
publishProgress("" + (int) ((float) i++ * 100 / aurl.length));
}
getFile returns a FileDownloadTask object, which is a subclass of Task . getFile返回FileDownloadTask对象,该对象是Task的子类。 As you probably know, this Task tracks the progress of the download.
您可能知道,此任务跟踪下载进度。 You have the option of kicking off all the downloads at once, collecting all the Tasks in a list, then using Tasks.whenAll() to get a new Task that completes when all the downloads are complete.
您可以选择立即开始所有下载,将所有任务收集在一个列表中,然后使用Tasks.whenAll()获得一个新的任务,该任务在所有下载完成后即完成。
I have a four part blog series about using Tasks that might help you better understand how they work. 我有一个由四个部分组成的博客系列,介绍如何使用任务,这些任务可能有助于您更好地了解它们的工作方式。
make childsRef hashmap and add a boolean to see if download is completed. 创建childsRef哈希图,并添加一个布尔值以查看下载是否完成。 set value true when onSuccess and make a handler to send message when a download completed to check if all boolean values true.
在onSuccess时将值设置为true,并在下载完成后使处理程序发送消息以检查所有布尔值是否为true。 than start your activity from handler.
而不是从处理程序开始您的活动。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.