您好我想在进度对话框中向用户显示文件上传进度,但进度对话框仅显示0%和100%(不显示实际进度)。 我正在使用改造文件上传和使用计数文件。
这是我正在使用的代码:
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();
请帮助我,提前致谢
编辑: 更改了发布进度数学等式:
publishProgress((int) ((num / (float) totalSizeValue) * 100));