I am uploading string and photo.and its working fine. Now I want to show progress bar while uploading data with percentage but percentage show very quickly to 100 percentage
protected class upload_images extends AsyncTask {
ProgressDialog progressDialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
// showDialog(progress_bar_type);
progressDialog = new ProgressDialog(Accept_Report.this);
progressDialog.setCancelable(false);
// dialog.setCanceledOnTouchOutside(false);
progressDialog.setIndeterminate(false);
// progressDialog.setMax(100);
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
// progressDialog.setProgress(0);
progressDialog.setMax(100);
// progressDialog.setMessage("Loading ...");
progressDialog.show();
// ProgressBar progressBar = (ProgressBar)findViewById(R.id.progressBar2);
}
@Override
protected String doInBackground(String... params) {
URL url;
HttpURLConnection connection = null;
String http = Util.URL + "reports/media/create";
try {
url = new URL(http);
connection = (HttpURLConnection) url.openConnection();
.
.
.
connection.connect();
...
// you are doing this
// what is jsonParam ?
//byte[] payload = jsonParam.toString().getBytes("UTF-8");
// how you gonna get content lenght from it?
int count = 0;
OutputStream wr = connection.getOutputStream();
InputStream inputStream = null;
byte[] payload = jsonParam.toString().getBytes("UTF-8");
int totalSze = payload.length;
Log.e("Total size ", "" + totalSze);
int bytesTransferred = 0;
int chunkSize = (2 * totalSze) / 100;
boolean last_loop = false;
// publishProgress(0);
...
// Do like this example
// getting file length
int lenghtOfFile = connection.getContentLength();
// input stream to read file - with 8k buffer
InputStream input = new BufferedInputStream(url.openStream(), 8192);
// Output stream to write file
OutputStream output = new FileOutputStream("/sdcard/downloadedfile.jpg");
byte data[] = new byte[1024];
long total = 0;
while ((count = input.read(data)) != -1) {
total += count;
// publishing the progress....
// After this onProgressUpdate will be called
publishProgress((int) ((total * 100) / lenghtOfFile));
// writing data to file
output.write(data, 0, count);
}
} catch (Exception e) {
}
return null;
}
@Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
// Log.e("dfsf",""+values[0]);
progressDialog.setProgress(values[0]);
// progressDialog.setProgress(values[0]);
}
@Override
protected void onPostExecute(String result) {
if (HttpResultimage == 204) {
progressDialog.dismiss();
}
}
}