【发布时间】:2019-01-08 16:33:54
【问题描述】:
GetImages getImages = new GetImages();
getImages.execute(keys);
private class GetImages extends AsyncTask(String, Void, Void){
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected Void doInBackground(String... keys) {
for(String key : keys){
StorageReference referenceImage = FirebaseStorage.getInstance().getReference().child("images/"+key);
final long ONE_MB = 1024*1024;
referenceImage.getBytes(ONE_MB).addOnSuccessListener(new OnSuccessListener() {
@Override
public void onSuccess(byte[] bytes) {
Toast.makeText(getApplicationContext(),"Success to Load Bitmap ....",Toast.LENGTH_LONG).show();
Bitmap bm = BitmapFactory.decodeByteArray(bytes,0,bytes.length);
}
});
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
Toast.makeText(getApplicationContext(),"Completed All Tasks ............",Toast.LENGTH_LONG).show();
}
}
}
代码的问题是 AsyncTask 在完成 doInBackGround() 之前执行 onPostExecute() 。 已完成所有任务... toast 在 Success to load bitmap toast 之前显示。我不明白问题是什么。
【问题讨论】:
-
您是否尝试删除`return null;`?
-
您正在为结果返回 null 并且在您的 postExceute 中变得无效。 ?
-
为什么在 doInBackground 中显示 toast。使用日志
-
我认为
doInBackground(String... keys)正在被调用,但您的OnSuccessListener的onSuccess()方法没有被调用。您可以通过在doInBackground(String... keys)方法中添加一些日志来验证吗? -
Firebase 操作已经是异步的。您无需将它们放在
AsyncTask中。这也是onPostExecute()在OnSuccessListener之前运行的原因。
标签: android firebase android-asynctask firebase-storage android-background