Reputation: 5990
I am uploading files from android to server, I am doing the upload with progress percentage overall for all files from 0 to 100%. All the files are uploading one by one. The known things are
I have a single progressbar to show overall percentage for all.
The uploading code is:
public abstract class UploadTask extends AsyncTask<String, Integer, String>
{
private Context mContext;
private Cursor mCursor;
private ProgressBar mProgressBar;
private int mItemCounter;
private int mFilesCount;
private int mUploaded;
private long mTotalSize;
public UploadTask(Context context, Cursor cursor, ProgressBar progressBar)
{
mContext = context;
mCursor = cursor;
mProgressBar = progressBar;
mFilesCount = cursor.getCount();
}
@Override
protected void onPreExecute()
{
super.onPreExecute();
mProgressBar.setMax(mFilesCount);
}
@Override
protected String doInBackground(String... params)
{
HttpClient httpClient = new DefaultHttpClient();
HttpContext httpContext = new BasicHttpContext();
while (mCursor.moveToNext())
{
String path = mCursor.getString(mCursor.getColumnIndex("_data"));
HttpPost httpPost = new HttpPost("http://...");
try
{
CustomMultiPartEntity multipartContent = new CustomMultiPartEntity(new CustomMultiPartEntity.ProgressListener()
{
@Override
public void transferred(long num)
{
publishProgress((int) (((((float) mUploaded) + ((float) num / (float) mTotalSize)) / (float) mFilesCount)));
}
});
multipartContent.addPart("userfile", new FileBody(new File(path), Utility.removeExtention(title), type, "UTF-8"));
mTotalSize = multipartContent.getContentLength();
httpPost.setEntity(multipartContent);
HttpResponse response = httpClient.execute(httpPost, httpContext);
InputStream inputStream = response.getEntity().getContent();
StringBuilder total = new StringBuilder();
String line;
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
try
{
while ((line = bufferedReader.readLine()) != null)
{
total.append(line);
}
inputStream.close();
bufferedReader.close();
}
catch (Exception e)
{
e.printStackTrace();
}
Log.wtf("response", total.toString() + " File Size: " + (mTotalSize / (1024)) + "KB");
}
catch (Exception e)
{
e.printStackTrace();
}
mUploaded++;
}
return null;
}
@Override
protected void onProgressUpdate(Integer... progress)
{
super.onProgressUpdate(progress);
mProgressBar.setProgress(progress[0]);
}
@Override
protected void onPostExecute(String s)
{
onCompleted(s == null);
}
protected abstract void onCompleted(boolean result);
}
Upvotes: 0
Views: 2446
Reputation: 11214
mProgressBar.setMax(mFilesCount);
. If you do that then you should use mFilesDownloaded in publishProgress. Otherwise start with mTotalBytesOfAllFiles and update with mTotalBytesUploaded.
Upvotes: 1