java - Monitoring BufferedInputStream download progress -
i'm trying download file using asynctask
on android. want display progressdialog
should have progress bar show status of download. i'm using onprogressupdate()
function , implemented call publishprogress()
in doinbackground()
function. however, progress dialog pops after downloading file. code:
protected long doinbackground(url...urls) { (int = 0; < urls.length; i++) { url = urls[i]; try { urlconnection conn = url.openconnection(); conn.connect(); totalsize = conn.getcontentlength(); bufferedinputstream bis = new bufferedinputstream(url.openstream()); fileoutputstream fos = new fileoutputstream(environment.getexternalstoragedirectory().getpath() + "/forvo_temp.mp3"); bufferedoutputstream bos = new bufferedoutputstream(fos,1024); byte [] data = new byte[1024]; int x=0; int c=0; while((x=bis.read(data,0,1024))>=0){ bos.write(data,0,x); c += 1024; publishprogress(c); } } catch (exception e) { e.printstacktrace(); } } return 0l; // don't know } protected void onprogressupdate(integer...args) { pd = progressdialog.show(context, "downloading...", "downloading...", true, false); pd.setprogress(args[0] / totalsize); }
i guess whole file downloaded when call new bufferedinputstream(url.openstream())
. how can monitor download progress?
this code useful showing download items totol size , downloaded size.
private static final int download_onprogress = 1; @override protected dialog oncreatedialog(int id) { switch (id) { case download_onprogress: progressdialog = new progressdialog(this); progressdialog.setmessage("downloading latest ..."); progressdialog.setcancelable(true); progressdialog.setprogressstyle(progressdialog.style_horizontal); try { progressdialog.show(); } catch (exception e) { // todo auto-generated catch block e.printstacktrace(); } return progressdialog; default: return null; } }
you can use asynctask downloading version in background.
private class download extends asynctask<string, string, string> { @override protected void onpreexecute() { // todo auto-generated method stub super.onpreexecute(); logger.info("loaddataasync onpreexecute"); showdialog(download_onprogress); } @override protected string doinbackground(string... aurl) { int count = 0; try { url url = new url(aurl[0]); urlconnection urlconnection = url.openconnection(); urlconnection.connect(); int contentlength = urlconnection.getcontentlength(); progressdialog.setmax(contentlength); string path = ""; file file = null; if (android.os.environment.getexternalstoragestate().equals( android.os.environment.media_mounted)) { path = environment.getexternalstoragedirectory() + "/download/"; file = new file(path); file.mkdirs(); file outputfile = new file(file, "telfaz.apk"); outputstream fos = new fileoutputstream(outputfile); inputstream = new bufferedinputstream(url.openstream()); byte[] buffer = new byte[1024]; long len1 = 0; while ((count = is.read(buffer)) != -1 && !download.iscancelled()) { len1 += count; publishprogress("" + len1); fos.write(buffer, 0, count); } fos.flush(); fos.close(); is.close(); } logger.info("success -> file downloaded succesfully. returning 'success' code"); return util.apk_download_success; } catch (ioexception e) { logger.error("exception in update process : " + util.getstacktrace(e)); } logger.info("failed -> file download failed. returning 'error' code"); return util.apk_download_failed; } @override protected void onpostexecute(string result) { logger.info("on download onpostexecute. result : " + result); progressdialog.dismiss(); removedialog(download_onprogress); if (result.equalsignorecase(util.apk_download_success)) { update(); } else { toast.maketext(downloadallcontentsactivity.this, getstring(r.string.updateapplicationfailed), toast.length_long).show(); loaddataasync.execute(); } } @override protected void onprogressupdate(string... values) { if (values != null && values.length > 0) { progressdialog.setprogress(integer.parseint(values[0])); } } }
Comments
Post a Comment