【问题标题】:AsyncTask is not working as expected in AndroidAsyncTask 在 Android 中没有按预期工作
【发布时间】: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) 正在被调用,但您的 OnSuccessListeneronSuccess() 方法没有被调用。您可以通过在doInBackground(String... keys) 方法中添加一些日志来验证吗?
  • Firebase 操作已经是异步的。您无需将它们放在AsyncTask 中。这也是onPostExecute()OnSuccessListener 之前运行的原因。

标签: android firebase android-asynctask firebase-storage android-background


【解决方案1】:

Firebase 方法referenceImage.getBytes 本身是异步的,您不需要将其放在 AsyncTask 中。获取图像后显示 Toast 可以这样做:

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);
     Toast.makeText(getApplicationContext(),"Completed  All Tasks 
     ............",Toast.LENGTH_LONG).show();
        }
     });

【讨论】:

  • 请查看更新后的问题。我想在所有图像加载后执行一些操作。
【解决方案2】:
GetImages getImages = new GetImages();
getImages.execute(key); 

密钥必须在 GetImages 类中初始化

protected void onPostExecute(Void aVoid) {
      Toast.makeText(getApplicationContext(),"Completed  All   Task .......",Toast.LENGTH_LONG).show();
            }

方法在doinbackground完成后运行,标记完成方法需要返回标志或数据。不返回 NULL

【讨论】:

  • 请查看更新后的问题。我尝试更改返回值,它不起作用。
  • Firebase 操作已经是异步的。 这个完美的答案
  • 好的,我明白了。但我想加载所有图像然后执行一些任务。我知道他们是我的代码的问题。你能给我任何解决方案吗?
猜你喜欢
  • 2011-12-14
  • 1970-01-01
  • 1970-01-01
  • 2014-01-26
  • 1970-01-01
  • 2021-10-19
  • 2020-03-18
  • 2012-06-14
  • 2014-11-15
相关资源
最近更新 更多