简体   繁体   English

小吃店不能立即显示

[英]Snackbar not showing instantly

i'm trying to save a image when an ImageView is clicked. 我正在尝试单击ImageView时保存图像。 Because the saving takes some time i want to have some sort of indicator, that the app is working. 因为保存需要一些时间,所以我想使用某种指示器,表明该应用程序正在运行。 I tried to use Snackbar for that. 我尝试使用Snackbar。 My code looks something like this: 我的代码如下所示:

File image = new File(directory, "image.png");

        if(!image.exists()){

            Snackbar bar = Snackbar.make(getActivity().findViewById(R.id.some_layout), "snackText", Snackbar.LENGTH_INDEFINITE);
            bar.show();

            FileOutputStream outStream = null;
            try {
               outStream = new FileOutputStream(image);
               bitmap.compress(Bitmap.CompressFormat.PNG, 100, outStream);
               outStream.flush();
               outStream.close();
            } catch (FileNotFoundException e) {
               e.printStackTrace();
            } catch (IOException e) {
               e.printStackTrace();
            }

            bar.dismiss();
        }

All that is inside the public void onClick(View view) of that ImageView. 该ImageView的public void onClick(View view)内部的所有内容。 The problem is, that the Snackbar only shows after the image is saved. 问题是,Snackbar仅在保存图像后显示。 I tried to force update the UI with invalidate() and tried it without starting a new Thread with the same result. 我试图用invalidate()强制更新UI,并尝试了不启动具有相同结果的新线程的情况。 Any ideas are greatly appreciated! 任何想法都将不胜感激!

Don't use snackbar here edit your code like this: if(!image.exists()){ 请勿在此处使用快餐栏,例如:if(!image.exists()){

        final ProgressDialog progressDialog =ProgressDialog.show(this, "","Please Wait...", true);

        Thread t = new Thread(new Runnable() {
            @Override
            public void run() {
                FileOutputStream outStream = null;
                try {
                    outStream = new FileOutputStream(image);
                    bitmap.compress(Bitmap.CompressFormat.PNG, 100, outStream);
                    outStream.flush();
                    outStream.close();
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        progressDialog.dismiss();
                    }
                });

            }

        });
        t.start();

        try {
            t.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM