2

我想显示一个下载位图的进度条:

    public static Bitmap getBitmapFromURL(String src) {
    try {
        URL url = new URL(src);
        HttpURLConnection connection = (HttpURLConnection) url
                .openConnection();
        connection.setDoInput(true);
        connection.connect();

        InputStream input = connection.getInputStream();

        int fileLength = connection.getContentLength();

        // download the file

        byte data[] = new byte[1024];
        long total = 0;
        int count;
        while ((count = input.read(data)) != -1) {
            total += count;
            // publishing the progress....
            progressDialog.setProgress((int) (total * 100 / fileLength));
            Log.i("progre+ss", (int) (total * 100 / fileLength) + " ");
        }

        Bitmap myBitmap = BitmapFactory.decodeStream(input);
        return myBitmap;
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
}

初始化为:

    private void openprogresdialog() {
    // TODO Auto-generated method stub
    progressDialog = new ProgressDialog(PrMapGen.this);
    progressDialog.setMessage("Generating File...");
    progressDialog.setCancelable(false);
    progressDialog.setIndeterminate(false);
    progressDialog.setMax(100);
    progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
    progressDialog.show();

    new Thread() {
        public void run() {
            try {
                generatePrMap(DataArray,
                        getIntent().getExtras().getString("project"));

            } catch (Exception e) {
            }
            progressDialog.dismiss();
            projdata.close();
            finish();
        }
    }.start();
}

我的问题如下:

当我使用while循环更新进度条时,没有返回图像,当我删除while循环时,位图成功返回。

为什么???

提前致谢

4

1 回答 1

2

这是一个简单的方法。

声明一个AsyncTask

将下载图像的代码放在doInBackgroud()中,并将 progressBar 设置在onUpdateProgress()中。

你也可以检查这个问题

于 2012-08-29T19:40:08.093 回答