你好,我正在尝试上传多个图像,等待它们返回,将下载的uri编译为一个对象,然后将其发送回我的活动。我将其用作上传firebase的参考。到目前为止,我有这个
private void saveStepWithImages(@NonNull Step step, Callback callback){
if(step.getStepId() == null){
Collection<Image> images = step.getImages().values();
List<Task<Uri>> taskArrayList= new ArrayList<>();
for (Image i: images) {
taskArrayList.add(uploadImageTask(new ImageUtils().StringToBitMap(i.getImageUrl()), i.getImageReference()));
}
Tasks.whenAll(taskArrayList).addOnCompleteListener(task -> {
Uri downloadUri = task.getResult(); // throws an error because task.getResult is void
});
}else{
updateStepInFirebase(step, callback);
}
}
以及在我的上传图片中
private Task<Uri> uploadImageTask(final Bitmap bitmap, String prepend){
final StorageReference ref = mStorageRef.child( prepend );
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
byte[] data = baos.toByteArray();
UploadTask uploadTask = ref.putBytes(data);
bitmap.recycle();
return uploadTask.continueWithTask(task -> {
bitmap.recycle();
return ref.getDownloadUrl();
});
}
Step是我创建的一个自定义对象,它包含一个带有字符串作为键且值为图像的图像映射。我的图像类如下
public class Image implements Parcelable {
private String imageUrl;
private String imageReference;
public void Image(){
}
//Setters and getters here;
}
任何建议将不胜感激。谢谢!
答案 0 :(得分:5)
解决此问题的关键是使用Tasks的whenAllSuccess()方法:
返回带有任务结果列表的任务,当所有指定任务成功完成时,任务结果列表将成功完成。
装有Tasks的whenAll()方法:
返回在所有指定任务都成功完成时成功完成的任务。
请参阅有关Tasks类的更多信息。
答案 1 :(得分:0)
您可以通过将所有调用嵌套在一个数组中,并将每个调用添加到firebase的Task API中来将多个文件上传到firebase:
定义参考和任务数组
StorageReference mStorageRef = FirebaseStorage.getInstance().getReference();
List<Task> myTasks = new ArrayList<>();
在此示例中,即时通讯使用的映射包含每个文件及其对应的存储目标
for (Map.Entry<String, Attachment> entry : storageRouteMap.entrySet()) {
String path = entry.getKey();
final Attachment localAtt = entry.getValue();
Uri fileUri = localAtt.getMyUri();
我将把每个任务放在任务数组中,对于一个文件,我有三个任务,一个任务用于上传文件,一个任务用于获取存储的url,另一个任务用于在实时数据库中写入元数据。
final StorageReference ref = mStorageRef.child(path);
ThreadPerTaskExecutor executor = new ThreadPerTaskExecutor();
UploadTask t1 = ref.putFile(fileUri);
myTasks.add(t1);
Task<Uri> t2 = t1.continueWithTask(executor,new Continuation<UploadTask.TaskSnapshot, Task<Uri>>() {
@Override
public Task<Uri> then(@NonNull Task<UploadTask.TaskSnapshot> task) throws Exception {
if (!task.isSuccessful()) {
throw task.getException();
}
return ref.getDownloadUrl();
}
});
myTasks.add(t2);
Task<Void> t3 = t2.continueWithTask(executor,new Continuation<Uri, Task<Void>>() {
@Override
public Task<Void> then(@NonNull Task<Uri> task) throws Exception {
if (!task.isSuccessful()) {
throw task.getException();
}
Attachment uploadAtt = new Attachment();
uploadAtt.name = localAtt.name;
uploadAtt.url = task.getResult().toString();
uploadAtt.type = localAtt.type;
String idAtt = UtilFirebase.getAttachmentReference().push().getKey();
UtilLog.LogToConsole(TAG," => "+postId+" => "+uidAuthor+" =>"+idAtt);
return UtilFirebase.getAttachmentReference()
.child(postId)
.child(uidAuthor)
.child(idAtt)
.setValue(uploadAtt);
}
}).continueWith(executor,new VideoTransaction(communityId,localAtt.size,localAtt.type));
myTasks.add(t3);
}
最后,我将查看所有任务是否都已完成或是否有错误,无论哪种方式都将结果传达给主线程。
Task finish = Tasks.whenAll((Collection) myTasks);
finish.addOnCompleteListener(new ThreadPerTaskExecutor(), new OnCompleteListener() {
@Override
public void onComplete(@NonNull Task task) {
if (task.isSuccessful()) {
callback.onComplete();
} else {
callback.onError(task.getException().toString());
}
}
});