private class ImageUpload extends AsyncTask<String,Integer,String> {
@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
}
}
Sunday, December 28, 2014
Android AsyncTask onProgressUpdate with ProgressDialog - How to get progress percentage of an uploading file?
Labels:
Android
Subscribe to:
Post Comments (Atom)
1 comment:
This information is impressive; I am inspired with your post writing style & how continuously you describe this topic. After reading your post, thanks for taking the time to discuss this, I feel happy about it and I love learning more about this topic.Android Training|Android Training in chennai with placement | Android Training in velachery
Post a Comment