Android文件上传进度条只显示0和100
嗨,我想在进度对话框中向用户显示文件上载进度,但进度对话框仅显示0%和100%(不显示实际进度)。 我使用的文件上传改造和使用计数文件 这是我正在使用的代码:Android文件上传进度条只显示0和100,android,file-upload,progressdialog,retrofit,Android,File Upload,Progressdialog,Retrofit,嗨,我想在进度对话框中向用户显示文件上载进度,但进度对话框仅显示0%和100%(不显示实际进度)。 我使用的文件上传改造和使用计数文件 这是我正在使用的代码: new AsyncTask<String, Integer, Void>() { private ProgressListener listener; private long totalSizeValue; private Progre
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();
(float)强制转换是不必要的,因为当您与100f相乘时,num将强制转换为float。这将导致totalSizeValue被浇铸为浮动。还有,你传递给publishProgress的价值观是什么?你确定它们在0到100之间吗?添加Log.v调用或调试它。我将在一分钟后发布日志。感谢您的回复。请检查:我无法获取@davidgiga1993日志,因此我对其进行了调试,并在每个步骤中评估了值,这些值是我获取的17,54100 pDialog.setProgress(progress[0]);正在使用值调用,但进度对话框未更新。请执行以下操作:
publishProgress((int) ((num / (float) totalSizeValue) * 100));