Hi i want to display file upload progress in a progress dialog to the user but the progress dialog is showing only 0% and 100% ( Not displaying the actual progress). I'm using retrofit for file upload and using counting file.
This is the code i am using:
new AsyncTask<String, Integer, Void>()
{
private ProgressListener listener;
private long totalSizeValue;
private ProgressDialog pDialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(ManualAdd.this);
pDialog.setMessage("Uploading...");
pDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
pDialog.setCancelable(false);
pDialog.show();
}
@Override
protected Void doInBackground(String... params) {
String realPath = MediaUtils.getRealPathFromURI(fileURI, ManualAdd.this);
File file = new File(realPath);
totalSizeValue = file.length();
final String filename = MediaUtils.getFileName(fileURI, ManualAdd.this);
listener = new ProgressListener() {
@Override
public void transferred(long num) {
publishProgress((int)(((float)num*100f)/(float)totalSizeValue));
}
};
final Attachment attachment = new Attachment();
attachment.setFileUri(fileURI);
final String filePath = realPath;
FileUploadService service = ServiceGenerator.createService(FileUploadService.class, FileUploadService.BASE_URL,Activityname);
Map<String,String> path = new HashMap<>();
path.put("path","fileUpload");
service.upload(path,new CountingTypedFile("image/"+MediaUtils.getFileExt(filename),file,listener), new Callback<Pk_Response>() {
@Override
public void success(Pk_Response pk_response, retrofit.client.Response response) {
// Success logic
pDialog.dismiss();
}
@Override
public void failure(RetrofitError error) {
Toast.makeText(activity,"Failed to upload attachments...",Toast.LENGTH_LONG).show();
pDialog.dismiss();
}
});
return null;
}
protected void onProgressUpdate(Integer... progress) {
pDialog.setProgress(progress[0]);
}
}.execute();
Please help me, Thanks in advance
EDIT: changed the publish progress math equation:
publishProgress((int) ((num / (float) totalSizeValue) * 100));