android - How to Show Progress Bar while uploading with percentage -


i uploading video , photo.and working fine .now want show progress bar while uploading data percentage.i took reference android hive , implemented in httpclient , modified accordingly httpurlconnection.any appreciated .thank you.

this main activity async task performing.

protected void onprogressupdate(integer... progress) {         // making progress bar visible         progressbar.setvisibility(view.visible);          // updating progress bar value         progressbar.setprogress(progress[0]);          // updating percentage value         txtpercentage.settext(string.valueof(progress[0]) + "%");     }      @override     protected string doinbackground(void... params) {         return uploadfile();     }      @suppresswarnings("deprecation")     private string uploadfile() {         string responsestring = null;             string charset = "utf-8";             string requesturl = "your_url";              videoupload multipart = null;             try {                 multipart = new videoupload(config.file_upload_url, charset);             } catch (ioexception e) {                 e.printstacktrace();             }          {             //========================================================             androidmultipartentity entity = new androidmultipartentity(                     new androidmultipartentity.progresslistener() {                          @override                         public void transferred(long num) {                             publishprogress((int) ((num / (float) totalsize) * 100));                         }                     });             //========================================================             multipart.addformfield("website", "www.androidhive.info");             multipart.addformfield("email", "abc@gmail.com");             try {                 multipart.addfilepart("image", new file(filepath));             } catch (ioexception e) {                 e.printstacktrace();             }              try {                 list<string> response = multipart.finish(); // response server.             } catch (ioexception e) {                 e.printstacktrace();             }         }          return responsestring;     }      @override     protected void onpostexecute(string result) {         log.e(tag, "response server: " + result);          // showing server response in alert dialog         showalert(result);          super.onpostexecute(result);     }  } 

this uploding class httpurlconnections , working .

public videoupload(string requesturl, string charset)         throws ioexception {     this.charset = charset;      // creates unique boundary based on time stamp     boundary = "===" + system.currenttimemillis() + "===";      url url = new url(requesturl);     httpconn = (httpurlconnection) url.openconnection();     httpconn.setusecaches(false);     httpconn.setdooutput(true); // indicates post method     httpconn.setdoinput(true);     httpconn.setrequestproperty("content-type",             "multipart/form-data; boundary=" + boundary);     httpconn.setrequestproperty("user-agent", "codejava agent");     httpconn.setrequestproperty("test", "bonjour");     outputstream = httpconn.getoutputstream();     writer = new printwriter(new outputstreamwriter(outputstream, charset),             true); }  /**  * adds form field request  *  * @param name  field name  * @param value field value  */ public void addformfield(string name, string value) {     writer.append("--" + boundary).append(line_feed);     writer.append("content-disposition: form-data; name=\"" + name + "\"")             .append(line_feed);     writer.append("content-type: text/plain; charset=" + charset).append(             line_feed);     writer.append(line_feed);     writer.append(value).append(line_feed);     writer.flush(); }  /**  * adds upload file section request  *  * @param fieldname  name attribute in <input type="file" name="..." />  * @param uploadfile file uploaded  * @throws ioexception  */ public void addfilepart(string fieldname, file uploadfile)         throws ioexception {     string filename = uploadfile.getname();     writer.append("--" + boundary).append(line_feed);     writer.append(             "content-disposition: form-data; name=\"" + fieldname                     + "\"; filename=\"" + filename + "\"")             .append(line_feed);     writer.append(             "content-type: "                     + urlconnection.guesscontenttypefromname(filename))             .append(line_feed);     writer.append("content-transfer-encoding: binary").append(line_feed);     writer.append(line_feed);     writer.flush();      fileinputstream inputstream = new fileinputstream(uploadfile);     byte[] buffer = new byte[4096];     int bytesread = -1;     while ((bytesread = inputstream.read(buffer)) != -1) {         outputstream.write(buffer, 0, bytesread);     }     outputstream.flush();     inputstream.close();      writer.append(line_feed);     writer.flush(); }  /**  * adds header field request.  *  * @param name  - name of header field  * @param value - value of header field  */ public void addheaderfield(string name, string value) {     writer.append(name + ": " + value).append(line_feed);     writer.flush(); }  /**  * completes request , receives response server.  *  * @return list of strings response in case server returned  * status ok, otherwise exception thrown.  * @throws ioexception  */ public list<string> finish() throws ioexception {     list<string> response = new arraylist<string>();      writer.append(line_feed).flush();     writer.append("--" + boundary + "--").append(line_feed);     writer.close();      // checks server's status code first     int status = httpconn.getresponsecode();     if (status == httpurlconnection.http_ok) {         bufferedreader reader = new bufferedreader(new inputstreamreader(                 httpconn.getinputstream()));         string line = null;         while ((line = reader.readline()) != null) {             response.add(line);         }         reader.close();         httpconn.disconnect();     } else {         throw new ioexception("server returned non-ok status: " + status);     }      return response; } 

here took progress bar class androidhive don't know how use

public class androidmultipartentity extends multipartentity   {  private final progresslistener listener;  public androidmultipartentity(final progresslistener listener) {     super();     this.listener = listener; }  public androidmultipartentity(final httpmultipartmode mode,         final progresslistener listener) {     super(mode);     this.listener = listener; }  public androidmultipartentity(httpmultipartmode mode, final string boundary,         final charset charset, final progresslistener listener) {     super(mode, boundary, charset);     this.listener = listener; }  @override public void writeto(final outputstream outstream) throws ioexception {     super.writeto(new countingoutputstream(outstream, this.listener)); }  public static interface progresslistener {     void transferred(long num); }  public static class countingoutputstream extends filteroutputstream {      private final progresslistener listener;     private long transferred;      public countingoutputstream(final outputstream out,             final progresslistener listener) {         super(out);         this.listener = listener;         this.transferred = 0;     }      public void write(byte[] b, int off, int len) throws ioexception {         out.write(b, off, len);         this.transferred += len;         this.listener.transferred(this.transferred);     }      public void write(int b) throws ioexception {         out.write(b);         this.transferred++;         this.listener.transferred(this.transferred);     } } } 

there progressupdate method in asynctask class, return progress of file uploading, here code might out.  private final progressdialog mdialog;  mdialog = new progressdialog(context);         mdialog.setmax(100);         mdialog.setmessage("uploading " + file.getname());         mdialog.setprogressstyle(progressdialog.style_horizontal);         mdialog.setprogress(0);         mdialog.setbutton("cancel", new dialoginterface.onclicklistener() {             public void onclick(dialoginterface dialog, int which) {                 // cancel putfile operation                 mrequest.abort();             }         });         mdialog.show();  @override     protected void onprogressupdate(long... progress) {         int percent = (int)(100.0*(double)progress[0]/mfilelen + 0.5);         mdialog.setprogress(percent);     } 

Comments