【问题标题】:ProgressDialog upade while image is downloading下载图像时 ProgressDialog 更新
【发布时间】:2023-03-05 00:07:01
【问题描述】:

我需要在 AsyncTask 中下载图像并在 progressDialog 中显示进度,我在这里唯一不能做的就是弄清楚如何使用 1024 字节的步骤正确更新进度条,目前我有这个并且它没有不工作

class DownloadImageTask extends AsyncTask<String, Integer, Void> {

    @Override
    protected Void doInBackground(String... url) {
        bitmap = DownloadImage(url[0]);
        return null;
    }

    @Override
    protected void onProgressUpdate(Integer... args) {
        mProgressDialog.setProgress(args[0]);
    }

    @Override
    protected void onPostExecute(Void unused) {
        ImageView img = (ImageView) findViewById(R.id.img);
        img.setImageBitmap(bitmap);
        img.setVisibility(View.INVISIBLE);              
        imgInfo = "Height: " + bitmap.getWidth() + " px" + "\n" + "Width: "
                + bitmap.getHeight() + " px" + "\n" + "File Size: "
                + fileSize / 1024 + " KB" + "\n" + "Image Name: "   
                + getString(R.string.img_name);             
        isDownloaded = true;          
        mProgressDialog.dismiss();
    }

    @Override
    protected void onPreExecute() {
        mProgressDialog = new ProgressDialog(AndroidActivity.this);
        mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        mProgressDialog.setMessage("Loading...");
        mProgressDialog.setCancelable(false);
        mProgressDialog.show();
    }

    private Bitmap DownloadImage(String URL) {
        InputStream in = null;
        try {
            in = OpenHttpConnection(URL);
            bitmap = BitmapFactory.decodeStream(in);
            in.close();
        } catch (IOException e1) {
            e1.printStackTrace();
        }
        return bitmap;
    }

    private InputStream OpenHttpConnection(String urlString)
            throws IOException {
        InputStream in = null;
        int response = -1;
        URL url = new URL(urlString);
        URLConnection conn = url.openConnection();
        try {
            HttpURLConnection httpConn = (HttpURLConnection) conn;
            httpConn.setAllowUserInteraction(false);
            httpConn.setInstanceFollowRedirects(true);
            httpConn.setRequestMethod("GET");
            httpConn.connect();
            fileSize = conn.getContentLength();
            response = httpConn.getResponseCode();
            if (response == HttpURLConnection.HTTP_OK) {
                in = httpConn.getInputStream();

                // loop with step 1kb
                byte data[] = new byte[1024];
                long total = 0;
                int count = 0;
                while ((count = in.read(data)) != -1) {
                    total += count;
                    publishProgress((int)(total*100/fileSize));
                } 

            }  
        } catch (Exception ex) {
            throw new IOException("Error connecting");
        }
        return in;
    }
}

有人可以帮忙吗?

【问题讨论】:

    标签: android download progressdialog


    【解决方案1】:

    您最好从doInBackground 返回位图并从onPostExecute() 中的参数中获取它

    class DownloadImageTask extends AsyncTask<String, Integer, Bitmap> {
    
    @Override
    protected Bitmap doInBackground(String... url) {
        Bitmap bitmap = DownloadImage(url[0]);
        return bitmap ;
    }
    
    @Override
    protected void onProgressUpdate(Integer... args) {
        mProgressDialog.setProgress(args[0]);
    }
    
    @Override
    protected void onPostExecute(Bitmap bmp) {
        ImageView img = (ImageView) findViewById(R.id.img);
        img.setImageBitmap(bmp);
        img.setVisibility(View.INVISIBLE);              
        imgInfo = "Height: " + bitmap.getWidth() + " px" + "\n" + "Width: "
                + bitmap.getHeight() + " px" + "\n" + "File Size: "
                + fileSize / 1024 + " KB" + "\n" + "Image Name: "   
                + getString(R.string.img_name);             
        isDownloaded = true;          
        mProgressDialog.dismiss();
    }
    

    **

    编辑:

    **
    一旦输入流到达终点,您将无法再从中读取数据。您应该将InputStream 写入byte array,然后将字节数组转换为位图。

    private Bitmap OpenHttpConnection(String urlString)
            throws IOException {
    ........
    ........
       byte data[] = new byte[1024];
       long total = 0;
       int count = 0;
       ByteArrayOutputStream bos = new ByteArrayOutputStream();
    
       while ((count = in.read(data)) != -1) {
           total += count;
           bos.write(data, 0, count);
           publishProgress((int)(total*100/fileSize));
       } 
    
       return BitmapFactory.decodeByteArray(bos.toByteArray(),0,bos.size());
    }
    

    【讨论】:

    • 所以我更改了代码,因此位图将进入参数并检查第 92 行的位图,当我在 OpenHttpConnection 中有代码部分时,它的空值是否更新了栏,如果我将其删除又正常了……
    • 所以问题是 while ((count = in.read(data)) != -1) { total += count;发布进度((int)(总计*100/fileSize)); } 不知何故,它弄乱了输入流,位图变为 null 任何人都知道如何修复它?
    • 我已经编辑了我的答案。您不能多次阅读InputStream
    • 那么你打算什么时候接受它作为答案。?请告诉我:)
    • @userSeven7s:private Bitmap OpenHttpConnection()...在哪里写,怎么称呼,它的作用是什么??
    【解决方案2】:

    在 doInBackground() 中,您必须使用 publishProgress() 来显示更新。 看这里的例子,publishProgress()的用法;
    http://developer.android.com/reference/android/os/AsyncTask.html

    【讨论】:

    • 我在 OpenHttpConnection 中使用 publishProgress(),女巫在 DdownloadImage 中,而在 doInBackground() 中:) 栏工作正常,但是当它完成下载时,我意外停止错误,LogCat 在 onPostExecute 中显示错误,如果我删除 OpenHttpConnection 中的更新进度部分,它可以在没有进度条的情况下正常工作
    • 那么你必须发布 logcat 错误才能知道它发生的原因。
    • 无法找到确切的位置,如果你能指出哪个statemnt是第92行,它很有用。猜测是,将progressDialog 的解除移到onPostExecute 的第一行并检查。您的对象之一在 postExecute 中为 null,progressDialog 或 ImageView img。
    • 好吧,第 92 行是 imgInfo = "Height: " + bitmap.getWidth() + " px" + "\n" + "Width: "+ bitmap.getHeight() + " px" + "\n" + "文件大小:" + fileSize / 1024 + "KB" + "\n" + "图像名称:" + getString(R.string.img_name);并移动解雇没有帮助:(
    • 只需检查该特定行中哪个(变量或对象)为空。
    猜你喜欢
    • 1970-01-01
    • 2017-03-15
    • 2016-07-26
    • 1970-01-01
    • 1970-01-01
    • 2016-03-02
    • 1970-01-01
    • 2012-12-21
    • 1970-01-01
    相关资源
    最近更新 更多