Reputation: 1340
I want to show a progressbar downloading a bitmap:
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;
}
}
which is initialised with:
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();
}
My problem is the following:
when I use the while loop to update the progressbar, no image is returned, when I delete the while loop the bitmap is returned successfuly.
Why???
thanks in advance
Upvotes: 2
Views: 1339
Reputation: 16354
Here is a tip to do it the easy way.
Declare an AsyncTask.
Put your code to download the image in the doInBackgroud() and set the progressBar inside onUpdateProgress().
You can check this question too.
Upvotes: 2