gpt4 book ai didi

java - 下载成功后返回方法

转载 作者:行者123 更新时间:2023-11-30 00:09:09 24 4
gpt4 key购买 nike

我正在尝试从 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();
}

最佳答案

我建议您使用 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 在任务完成时调用。它的公共(public)方法 onComplete 返回完成的任务(永不为空)。例如,

addOnCompleteListener(new OnCompleteListener<byte[]>() {
@Override
public void onComplete(@NonNull Task<byte[]> task) {
//here, task is completed task returned by onComplete.
}
});

OnSuccessListener 在任务成功完成时调用。它的方法公共(public)方法 onSuccess 返回任务的结果。例如,

addOnSuccessListener(new OnSuccessListener<byte[]>() {
@Override
public void onSuccess(byte[] bytes) {
//here, bytes are the result that you are downloading or uploading
}
});

关于java - 下载成功后返回方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48473436/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com