java - How to calculate uploading task's percentage -


i have dofileupload method in uploadasync class extends asynctask. want know how calculate uploading task's percentage following code update progressdialog using publishprogress(""+values)

 private void dofileupload(){         httpurlconnection conn = null;         dataoutputstream dos = null;         datainputstream instream = null;          string exsistingfilename = "/sdcard/six.3gp";         // place doing wrong.         string lineend = "\r\n";         string twohyphens = "--";         string boundary =  "*****";         int bytesread, bytesavailable, buffersize;         byte[] buffer;         int maxbuffersize = 1*1024*1024;         string urlstring = "http://192.168.1.5/upload.php";         try         {             log.e("mediaplayer","inside second method");             fileinputstream fileinputstream = new fileinputstream(new file(exsistingfilename) );             url url = new url(urlstring);             conn = (httpurlconnection) url.openconnection();             conn.setdoinput(true);             // allow outputs             conn.setdooutput(true);             // don't use cached copy.             conn.setusecaches(false);             // use post method.             conn.setrequestmethod("post");             conn.setrequestproperty("connection", "keep-alive");             conn.setrequestproperty("content-type", "multipart/form-data;boundary="+boundary);             dos = new dataoutputstream( conn.getoutputstream() );             dos.writebytes(twohyphens + boundary + lineend);             dos.writebytes("content-disposition: form-data; name=\"uploadedfile\";filename=\"" + exsistingfilename +"\"" + lineend);             dos.writebytes(lineend);             log.e("mediaplayer","headers written");             bytesavailable = fileinputstream.available();             buffersize = math.min(bytesavailable, maxbuffersize);             buffer = new byte[buffersize];             bytesread = fileinputstream.read(buffer, 0, buffersize);             while (bytesread > 0)             {                 dos.write(buffer, 0, buffersize);                 bytesavailable = fileinputstream.available();                 buffersize = math.min(bytesavailable, maxbuffersize);                 bytesread = fileinputstream.read(buffer, 0, buffersize);             }             dos.writebytes(lineend);             dos.writebytes(twohyphens + boundary + twohyphens + lineend);             bufferedreader in = new bufferedreader(new inputstreamreader(conn.getinputstream()));             string inputline;             while ((inputline = in.readline()) != null)                  tv.append(inputline);             // close streams             log.e("mediaplayer","file written");             fileinputstream.close();             dos.flush();             dos.close();         }         catch (malformedurlexception ex)         {             log.e("mediaplayer", "error: " + ex.getmessage(), ex);         }         catch (ioexception ioe)         {             log.e("mediaplayer", "error: " + ioe.getmessage(), ioe);         }          //------------------ read server response         try {             instream = new datainputstream ( conn.getinputstream() );             string str;                         while (( str = instream.readline()) != null)             {                 log.e("mediaplayer","server response"+str);             }             /*while((str = instream.readline()) !=null ){              }*/             instream.close();         }         catch (ioexception ioex){             log.e("mediaplayer", "error: " + ioex.getmessage(), ioex);         }     } 

thanks

i had similar issue once, used cursor track bytes downloaded mentioned here: show download progress inside activity using downloadmanager


Comments