Android 下载位图时显示进度对话框
我想在下载位图时显示进度条:Android 下载位图时显示进度对话框,android,bitmap,progress-bar,Android,Bitmap,Progress Bar,我想在下载位图时显示进度条: public static Bitmap getBitmapFromURL(String src) { try { URL url = new URL(src); HttpURLConnection connection = (HttpURLConnection) url .openConnection(); connection.setDoInput(true);
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循环更新progressbar时,不会返回任何图像,当我删除while循环时,位图会成功返回
为什么
提前感谢这里有一个简单方法的提示 宣布 将用于下载图像的代码放入doinbackground()中,并在onUpdateProgress()中设置进度条 你也可以查一下