title: Android AsyncTask onProgressUpdate - How to get progress percentage of an uploading file?
body: <p>I'm using an Async task and using onProgressUpdate for uploading an image. It is running fine, but I'm facing problem in getting the percentage of file that is being uploaded.How can I get to know how much percentage of the file is currently uploaded. 
  Below is the code I have used.</p>
  
  <pre><code>private class ImageUpload extends AsyncTask&lt;String,Integer,String&gt; {
  
          @Override
          protected void onProgressUpdate(Integer... progress) {
              // TODO Auto-generated method stub
  
              mProgressDialog.setProgress((int) (progress[0]));
          }
  
          @Override
          protected String doInBackground( String... params ) {
  
  
              String result = "";
  
  
              HttpClient httpClient = new DefaultHttpClient();
  
  
              HttpPost httpPostRequest = new HttpPost( "http://192.168.212.177:8089/services/imageUpload" );
  
              try {
                  Date todaysdate = new Date();
                  for ( String imageFile : params ) {
                      Bitmap bm = BitmapFactory.decodeFile(imageFile);
                      ByteArrayOutputStream baos = new ByteArrayOutputStream();  
                      bm.compress(Bitmap.CompressFormat.JPEG, 15, baos); //bm is the bitmap object   
                      byte[] b = baos.toByteArray(); 
                      String encodedImage = Base64.encodeToString(b, Base64.DEFAULT);
  
                      FileBody bin = new FileBody( file );
  
                      MultipartEntityBuilder multiPartEntityBuilder = MultipartEntityBuilder.create();
  
                      int strIndex = imageFile.lastIndexOf( "/" );
                      String imageName = imageFile.substring( strIndex + 1 , imageFile.length() );
  
                      multiPartEntityBuilder.addPart( "imageFile" , new StringBody(encodedImage , ContentType.TEXT_PLAIN ) );
                      multiPartEntityBuilder.addPart( "filename" , new StringBody( farmersEnteredNumber + "_" + datemill.getTime() , ContentType.TEXT_PLAIN ) );
                      multiPartEntityBuilder.addPart( "imageName" , new StringBody( imageName, ContentType.TEXT_PLAIN ) );
  
  
                      long total = 0;
  
                      long lenghtOfFile = multiPartEntityBuilder.build().getContentLength();
                      publishProgress((int)((total*100)/lenghtOfFile));
                      httpPostRequest.setEntity( multiPartEntityBuilder.build() );
  
  
                      HttpResponse httpResponse = null;
                      httpResponse = httpClient.execute( httpPostRequest );
  
  
                      InputStream inputStream = null;
                      inputStream = httpResponse.getEntity().getContent();
  
                      if ( inputStream != null )
                          result = convertInputStreamToString( inputStream );
                      else
                          result = "Did not work!";
                      return result;
                  }
              } catch ( Exception e ) {
  
                  return null;
              }
  
              return result;
          }
  
          @Override
          protected void onPreExecute() {
  
              // TODO Auto-generated method stub
              super.onPreExecute();
              FarmerDetails.this.runOnUiThread( new Runnable() {
  
                  public void run() {
  
                      mProgressDialog = new ProgressDialog( FarmerDetails.this );
                      mProgressDialog.setTitle( "Image Uploading" );
                      mProgressDialog.setMessage( "Uploading Image Please Wait" );
                      mProgressDialog.setIndeterminate( false );
                      mProgressDialog.setMax(100);
                      mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
                      mProgressDialog.setCancelable(true);
                      mProgressDialog.show();
  
                  }
              } );
  
  
  
          }
  
          @Override
          protected void onPostExecute( String result ) {
              super.onPostExecute( result );
              mProgressDialog.dismiss();
              // TODO Auto-generated method stub
  
  
          }
  
      }
  </code></pre>