【问题标题】:Show ProgressDialog while image downloading下载图像时显示 ProgressDialog
【发布时间】:2017-06-27 22:16:49
【问题描述】:

我的应用程序具有下载功能需要一些时间来下载图像,因为我在下载图像时尝试添加ProgressDialogProgressDialog它永远不会出现请帮助我

这是我保存图像的代码:

private String saveImage(Bitmap image) {
    ProgressDialog pd = new ProgressDialog(Pop.this);
    pd.setMessage("Downloading ...");
    pd.show();
    String savedImagePath = null;
    String imageFileName = "vapor"+System.currentTimeMillis()+ ".png";
    File storageDir = new File(
            Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)
                    + "/vaporwave");
    boolean success = true;
    if (!storageDir.exists()) {
        success = storageDir.mkdirs();
    }
    if (success) {
        File imageFile = new File(storageDir, imageFileName);
        savedImagePath = imageFile.getAbsolutePath();
        try {
            OutputStream fOut = new FileOutputStream(imageFile);
            image.compress(Bitmap.CompressFormat.PNG, 100, fOut);
            fOut.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        // Add the image to the system gallery
        galleryAddPic(savedImagePath);
        Toast.makeText(Pop.this, "IMAGE SAVED", Toast.LENGTH_LONG).show();
        pd.dismiss();
    }
    return savedImagePath;
}

【问题讨论】:

    标签: android image progressdialog


    【解决方案1】:

    你应该使用像AsyncTask这样的后台任务,你不应该在你的UI线程上做复杂的任务。(否则你的UI会很幸运直到任务完成,所以你的progressDialog永远不会显示)

    您可以阅读 AsyncTask Here

    例如在异步中:

    1) 在onPreExecute 上显示你的progressDialog

    2) 并在onPostExecute 中将其关闭

    3) 并在doInBackground 中完成您的工作(保存图像)(此方法中的返回类型将传递给onPostExecute

    public class saveImage extends AsyncTask<Bitmap,Void,String>{
        ProgressDialog pd;
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pd = new ProgressDialog(Pop.this);
            pd.setMessage("Downloading ...");
            pd.show();
        }
    
        @Override
        protected String doInBackground(Bitmap... params) {
            Bitmap image = params[0];
            String savedImagePath = null;
            String imageFileName = "vapor"+System.currentTimeMillis()+ ".png";
            File storageDir = new File(
                    Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)
                            + "/vaporwave");
            boolean success = true;
            if (!storageDir.exists()) {
                success = storageDir.mkdirs();
            }
            if (success) {
                File imageFile = new File(storageDir, imageFileName);
                savedImagePath = imageFile.getAbsolutePath();
                try {
                    OutputStream fOut = new FileOutputStream(imageFile);
                    image.compress(Bitmap.CompressFormat.PNG, 100, fOut);
                    fOut.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                // Add the image to the system gallery
                galleryAddPic(savedImagePath);
                Toast.makeText(Pop.this, "IMAGE SAVED", Toast.LENGTH_LONG).show();
                pd.dismiss();
            }
            return savedImagePath;
        }
    
        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            //s is your savedImagePath
            pd.dismiss();
        }
    }
    

    你可以这样调用这个任务:

        new saveImage().execute(yourBitmap);
    

    【讨论】:

    • 感谢您的回答,我在image.compress(Bitmap.CompressFormat.PNG, 100, fOut); 中遇到了Bitmap Image 的问题,我也遇到了`return savedImagePath;` 的问题,我把我所有的代码都放在了doInBackground 上
    • 我根据您的代码更改了答案,请参阅更新的 saveImage 任务。
    猜你喜欢
    • 2023-03-05
    • 2014-06-11
    • 2012-07-17
    • 1970-01-01
    • 2013-10-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-31
    相关资源
    最近更新 更多