Android Studio-UI线程未使用异步任务更新

我有以下问题:我有一个移动应用程序,单击按钮(“是按钮”),即可在异步任务中从服务器中获取数据(工作正常)。当该任务运行时,我希望我的应用显示一个布局(框架布局),该布局在创建时设置为不可见,并且包含一个textview和一个Circle Progressbar。所以我将行设置为可见的行放入Asnyc任务的onPreExecute方法中,因为我读到此方法将在UI线程上执行。

但是此框架布局仅在我的整个代码运行后显示,而在执行时不显示。

这是我的代码,我从代码中删除了我认为不存在问题的部分,以使您更容易理解。

public class KeepImageactivity extends AppCompatactivity {

    private static final int PORT_NO = 1234;
    private String pathname;
    private File imgFile;
    private Button yesbutton;
    private Button nobutton;
    private ImageView imageView;
    private FrameLayout frameLayout;


    @SuppressLint("SourceLockedOrientationactivity")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRequestedOrientation(activityInfo.SCREEN_ORIENTATION_PORTRAIT);
        setContentView(R.layout.activity_keep_image);

        Intent receivedIntent = getIntent();
        pathname = receivedIntent.getStringExtra(Cameraactivity.EXTRA_MESSAGE);
        imgFile = new File(pathname);

        imageView = findViewById(R.id.imageViewID);
        yesbutton = findViewById(R.id.yesbuttonID);
        nobutton = findViewById(R.id.discardbuttonID);

        nobutton.setOnClicklistener(new View.OnClicklistener() {
            @Override
            public void onClick(View view) {
                onDiscard(pathname);
            }
        });
        yesbutton.setOnClicklistener(new View.OnClicklistener() {
            @RequiresApi(api = Build.VERSION_CODES.O)
            @Override
            public void onClick(View view) {
                onYes();
            }
        });

        frameLayout = findViewById(R.id.framelayout);
        frameLayout.setVisibility(View.GONE);
        showImage();
    }

    //method to show a picture in an ImageView
    private void showImage() {
        imgFile = new File(pathname);
        Bitmap bitmap = BitmapFactory.decodeFile(imgFile.getabsolutePath());
        imageView.setRotation(90);
        imageView.setImageBitmap(bitmap);
    }

    private void onDiscard(String pathname) {
        //....not relevant
}


    @RequiresApi(api = Build.VERSION_CODES.O)
    private void onYes() {

        //doInBackground just fetches data and writes to jpg
        class ConnectTask extends AsyncTask<Void,Void,Void> {
            @Override
            protected Void doInBackground(Void... voids) {
                //doing stuff (connection to server and downloading image ("image_from_server"))
            }

            @Override
            //here my frameLayout is not being set to Visible,at least not DURING execution.
            protected void onPreExecute() {
                frameLayout.setVisibility(View.VISIBLE);

            }

            @Override
            protected void onPostExecute(Void aVoid) {

            }

            @Override
            protected void onProgressUpdate(Void... values) {
            }
        }



        ConnectTask connect =  new ConnectTask();
        Void[] param = null;

        //execute Async task
        connect.execute(param);


        //wait until picture on Phone
        File xaifile = new File(Environment.getExternalStorageDirectory() + "/image_from_server.jpg");
        while (true){
            if (xaifile.exists()){
                break;
            }
        }
        replacePicture(xaifile);
    }


    //here i replace the picture/bitmap in the ImageView with another one (that just came from server)
    private void replacePicture(File xaifile) {
        Bitmap bitmap = BitmapFactory.decodeFile(Environment.getExternalStorageDirectory() + "/image_from_server.jpg");
        imageView.setRotation(90);
        imageView.setImageBitmap(bitmap);

        //finished loading,I commented this line to see if my Layout would show afterwards. It does.
        //frameLayout.setVisibility(View.INVISIBLE);

        //delete both files,not needed anymore
        xaifile.delete();
        imgFile.delete();
    }

//AND IT SEEMS LIKE HERE IS THE MOMENT THAT THE UPDATE TO THE UI IS SHOWN. WHY??
}
iCMS 回答:Android Studio-UI线程未使用异步任务更新

编写此部分:

//wait until picture on Phone
    File xaifile = new File(Environment.getExternalStorageDirectory() + "/image_from_server.jpg");
    while (true){
        if (xaifile.exists()){
            break;
        }
    }
    replacePicture(xaifile);

在onPostExecute

当前您正在阻止主线程。

本文链接:https://www.f2er.com/1954557.html

大家都在问