Note that there are some explanatory texts on larger screens.

plurals
  1. POProgressDialog upade while image is downloading
    text
    copied!<p>I need to download a image in a AsyncTask and display progress in a progressDialog, the only thing i cant manage to do here is figure out how to update the progressbar properly with a 1024 bytes step, currently i have this and it doesn't work</p> <pre><code>class DownloadImageTask extends AsyncTask&lt;String, Integer, Void&gt; { @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; } } </code></pre> <p>can someone help ?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload