android - ProgressDialog upade while image is downloading -
I need to download an image in an asyncTask and display progress in one progress, the only thing that I do here Can not manage how to properly update the progress bar with a 1024 bytes phase, I currently have this and it does not work
Class DownloadImageTask AsyncTask and String, Integer, null & gt; {@Override Safe Wide Doign Background (string ... url) {bitmap = DownloadImage (url [0]); Return tap; } @ Override progress update on protected void (integer ... args) {mProgressDialog.setProgress (args [0]); } @ Override Protected Zero at PostXextack (Zero Unused) {Image View IMG = (Image View) SearchVBIID (RID.img); Img.setImageBitmap (bitmap); Img.setVisibility (View.INVISIBLE); ImgInfo = "height:" + bitmap.getWidth () + "px" + "\ n" + "width:" + bitmap.getHeight () + "px" + "\ n" + "File size:" + file Size / 1024 + "KB" + "\ n" + "Image Name:" + + Meet (R. string.IMG_Name); Downloaded = True; MProgressDialog.dismiss (); } @ Override Prescott () {mProgressDialog = New Progress DILOG (Android Attachment. This) at Protected Zero; MProgressDialog.setProgressStyle (ProgressDialog.STYLE_HORIZONTAL); MProgressDialog.setMessage ("Loading ..."); MProgressDialog.setCancelable (wrong); MProgressDialog.show (); } Private bitmap download image (string url) {InputStream in = null; Try it in {OpenHttpConnection (URL); Bitmap = bitmapfender Ecodestream (in); In.close (); } Hold (IOException e1) {e1.printStackTrace (); } Return Bitmap; } PrivateInststream OpenHttpConnect (string URLstring) throws IOException {InputStream in = null; Acute reaction = -1; Url url = new url (url string); URL Connection Connection = url.openConnection (); Try {HttpURLConnection httpConn = (HttpURLConnection) conn; HttpConn.setAllowUserInteraction (wrong); HttpConn.setInstanceFollowRedirects (true); HttpConn.setRequestMethod ("GET"); HttpConn.connect (); FileSize = conn.getContentLength (); Response = httpConn.getResponseCode (); If (feedback == HttpURLConnection.HTTP_OK) {in = httpConn.getInputStream (); // loop [] = new byte with 1024 step by step 1 KB byte [1024]; Long total = 0; Int count = 0; While ((count = in.read (data))! = -1) {total + = count; PublishProgress ((int) (total * 100 / FILESIZE)); }}} Hold (exception before) {new IOException ("Error connecting"); } Return; }} Can anyone help?
You get better code from doInBackground and onPostExecute () class. ImageImageTask Expands AsyncTask & lt; String, integer, bitmap & gt; {@Override Safe Bitmap doInBackground (string ... url) {bitmap bitmap = download image (url [0]); Return bitmap; } @ Override progress update on protected void (integer ... args) {mProgressDialog.setProgress (args [0]); } @ Override Protected Zero at PostXextack (bitmap BMP) {Image View IMG = (Image View) SearchVBIID (RIDIMG); Img.setImageBitmap (BMP); Img.setVisibility (View.INVISIBLE); ImgInfo = "height:" + bitmap.getWidth () + "px" + "\ n" + "width:" + bitmap.getHeight () + "px" + "\ n" + "File size:" + file Size / 1024 + "KB" + "\ n" + "Image Name:" + + Meet (R. string.IMG_Name); Downloaded = True; MProgressDialog.dismiss (); } **
Edit:
**
Once the inputstream reaches its end, you can now access it from Instead you can not read data, instead you should type the InputStream in a byte array and then convert the byteearray to a bitmap. Private bitmap throws open HTTech connections (IOException) ........ ........ byte data [] = new byte [1024]; Long total = 0; Int count = 0; Bytereon OutputStream Boss = New Bytereunootstream (); While ((count = in.read (data))! = -1) {total + = count; boss. Write (data, 0, count); PublishProgress ((int) (total * 100 / FILESIZE)); } Return BitMapficht. Decodeabiteure (bos. betaarerere), 0, bose size ()); }
Comments
Post a Comment