我正在尝试从firebase存储中下载图像并在下载后显示图像。 有没有办法在下载完成后返回我的方法?
这是我方法的代码:
public void downloadImagesFromFireStorage(final String couponName, String imgName) {
finish = false;
StorageReference storageRef = FirebaseStorage.getInstance()
.getReferenceFromUrl("gs://xxxxx.appspot.com").child("imgs").child(imgName);
final long ONE_MEGABYTE = 1024 * 1024;
storageRef.getBytes(ONE_MEGABYTE).addOnSuccessListener(new OnSuccessListener<byte[]>() {
@Override
public void onSuccess(byte[] bytes) {
Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
saveBitmap(couponName, bitmap);
}
});
final ProgressDialog progressDialog = new ProgressDialog(context);
progressDialog.setTitle("Downloading..");
progressDialog.setMessage("Now download " + couponName + "....");
progressDialog.show();
}
答案 0 :(得分:0)
是的,你可以,但我不推荐它,你可以添加while循环,在finish
标志变为true之前不做任何事情,你需要在onFailure
上听取更改{{ 1}}标志
finish
答案 1 :(得分:0)
我建议您使用OnCompleteListener
来确定任务是否完成。例如,
StorageReference storageRef = FirebaseStorage.getInstance()
.getReferenceFromUrl("gs://xxxxx.appspot.com").child("imgs");
final long ONE_MEGABYTE = 1024 * 1024;
storageRef.getBytes(ONE_MEGABYTE).addOnSuccessListener(this, new OnSuccessListener<byte[]>() {
@Override
public void onSuccess(byte[] bytes) {
}
}).addOnCompleteListener(this, new OnCompleteListener<byte[]>() {
@Override
public void onComplete(@NonNull Task<byte[]> task) {
if (task.isSuccessful()){
// do your work
}
}
});
修改强>
根据Firebase文档,OnCompleteListener
在任务完成时调用。它的公共方法onComplete
返回已完成的任务(永不为null)。例如,
addOnCompleteListener(new OnCompleteListener<byte[]>() {
@Override
public void onComplete(@NonNull Task<byte[]> task) {
//here, task is completed task returned by onComplete.
}
});
当任务成功完成时,OnSuccessListener
被调用。它的方法是公共方法onSuccess
返回任务的结果。例如,
addOnSuccessListener(new OnSuccessListener<byte[]>() {
@Override
public void onSuccess(byte[] bytes) {
//here, bytes are the result that you are downloading or uploading
}
});
答案 2 :(得分:0)
我宁愿使用SettableFuture
来返回ListenableFuture
。
private ListenableFuture<Void> downloadImage() {
final SettableFuture<Void> future = SettableFuture.create();
StorageReference storageRef = FirebaseStorage.getInstance()
.getReferenceFromUrl("gs://xxxxx.appspot.com").child("imgs").child(imgName);
final long ONE_MEGABYTE = 1024 * 1024;
//performing the task on a background thread
threadPoolService.schedule(new Runnable() {
@Override
public void run() {
storageRef.getBytes(ONE_MEGABYTE).addOnSuccessListener(new OnSuccessListener<byte[]>() {
@Override
public void onSuccess(byte[] bytes) {
Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
saveBitmap(couponName, bitmap);
future.set(null);
}
});
}
});
return future;
}
并在结果可用时添加回调。
final ProgressDialog progressDialog = new ProgressDialog(context);
progressDialog.setTitle("Downloading..");
progressDialog.setMessage("Now download " + couponName + "....");
progressDialog.show();
Futures.addCallback(downloadImage(), new FutureCallback<Void>() {
@Override
public void onSuccess(@Nullable Void result) {
progressDialog.dismiss();
}
@Override
public void onFailure(Throwable t) {
progressDialog.dismiss();
Timber.e(t, "Error occurred while downloading image");
}
});
如果要下载多个图像,请为每个图像创建未来,例如
List<SettableFuture<Void>> futures = new ArrayList<>();
for() {
futures.add(downloadImage())
}
然后在下载所有图像时附加回调:
Futures.successfulAsList