Android AsyncTask publishProgress未更新对话框
我正在尝试更新一个进度对话框,以增加大约1-100%。然而,没有任何东西会增加。它总是说0。尽管如此,我可以在我的日志猫中看到打印出来的数字,并且在100之后,一切都会在应该结束的时候结束 有人看到我错过了什么吗?提前感谢您的帮助Android AsyncTask publishProgress未更新对话框,android,Android,我正在尝试更新一个进度对话框,以增加大约1-100%。然而,没有任何东西会增加。它总是说0。尽管如此,我可以在我的日志猫中看到打印出来的数字,并且在100之后,一切都会在应该结束的时候结束 有人看到我错过了什么吗?提前感谢您的帮助 private class DownLoadSigTask extends AsyncTask<String, Integer, String> { private final ProgressDialog dialog = new Progre
private class DownLoadSigTask extends AsyncTask<String, Integer, String> {
private final ProgressDialog dialog = new ProgressDialog(NasStorageNasList.this);
// Set the fileName and filesize to be used later
String fileName = (String) recordItem.get(mPresentDown).get("name");
String filesize = (String) recordItem.get(mPresentDown).get("filesize");
// can use UI thread here
@Override
protected void onPreExecute() {
this.dialog.setMessage("Downloading " + fileName + " from the server. Please wait.");
this.dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
this.dialog.setCancelable(false);
this.dialog.setCanceledOnTouchOutside(false);
this.dialog.show();
}
// automatically done on worker thread (separate from UI thread)
@Override
protected String doInBackground(final String... args) {
// Here is where we need to do the downloading of the
try {
File destDir = new File(LOCALDOWNROOT);
if (!destDir.exists()) {
destDir.mkdirs();
}
File outputFile = new File(destDir, fileName);
Log.e("What is the filename path to download? ", REMOTEDOWNROOT + DATADIRECTORY + fileName);
InputStream fis = sardine.get(REMOTEDOWNROOT + DATADIRECTORY + "/"
+ fileName.replace(" ", "%20"));
FileOutputStream fos = new FileOutputStream(outputFile);
byte[] buffer = new byte[1444];
Log.e("What is the length of the File Size? ", filesize);
long total = 0;
int byteread = 0;
while ((byteread = fis.read(buffer)) != -1) {
downloadTotal += byteread;
// Here is the WHILE LOOP that we will want to give the user
publishProgress((int)(total*100/Long.parseLong(filesize)));
Log.e("CurrentAmount Downloaded: ", String.valueOf((int)(downloadTotal*100/Long.parseLong(filesize))));
// How can I tell the UI the downloaded percentage?
// So, far I am updating the log cat...but nothing for the user.
fos.write(buffer, 0, byteread);
}
fis.close();
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
// Just Return null because the void task
return null;
}
// add in a progress bar update
@Override
protected void onProgressUpdate(Integer...progress) {
this.dialog.setProgress(progress[0]);
}
// can use UI thread here
@Override
protected void onPostExecute(final String errMsg) {
if (this.dialog.isShowing()) {
this.dialog.dismiss();
}
Toast.makeText(NasStorageNasList.this, "File Download Done!",
Toast.LENGTH_SHORT).show();
}
}// end DownloadSigTask
private类DownLoadSigTask扩展异步任务{
private final ProgressDialog=新建ProgressDialog(naStorageNaslist.this);
//设置稍后使用的文件名和文件大小
字符串文件名=(字符串)recordItem.get(mPresentDown.get(“名称”);
String filesize=(String)recordItem.get(mPresentDown.get(“filesize”);
//可以在这里使用UI线程
@凌驾
受保护的void onPreExecute(){
this.dialog.setMessage(“正在从服务器下载”+fileName+”,请稍候。”);
this.dialog.setProgressStyle(ProgressDialog.STYLE_水平);
this.dialog.setCancelable(false);
this.dialog.setCanceledOnTouchOutside(false);
this.dialog.show();
}
//在工作线程上自动完成(与UI线程分开)
@凌驾
受保护字符串doInBackground(最终字符串…args){
//这里是我们需要下载的地方
试一试{
File destDir=新文件(LOCALDOWNROOT);
如果(!destDir.exists()){
destDir.mkdirs();
}
File outputFile=新文件(destDir,文件名);
Log.e(“要下载的文件名路径是什么?”,REMOTEDOWNROOT+DATADIRECTORY+filename);
InputStream fis=sardine.get(REMOTEDOWNROOT+DATADIRECTORY+“/”
+文件名。替换(“,“%20”);
FileOutputStream fos=新的FileOutputStream(outputFile);
字节[]缓冲区=新字节[1444];
Log.e(“文件大小的长度是多少?”,filesize);
长总计=0;
int byteread=0;
而((byteread=fis.read(buffer))!=-1){
下载总量+=字节数;
//下面是我们想要给用户的WHILE循环
publishProgress((int)(总计*100/Long.parseLong(文件大小));
Log.e(“当前下载量:”,String.valueOf((int)(downloadtoal*100/Long.parseLong(filesize)));
//我如何告诉用户界面下载的百分比?
//到目前为止,我正在更新日志猫…但没有为用户更新任何内容。
fos.写入(缓冲区,0,字节);
}
fis.close();
fos.close();
}捕获(IOE异常){
e、 printStackTrace();
}
//只需返回null,因为该任务无效
返回null;
}
//添加进度条更新
@凌驾
受保护的void onProgressUpdate(整数…进度){
this.dialog.setProgress(progress[0]);
}
//可以在这里使用UI线程
@凌驾
受保护的void onPostExecute(最终字符串errMsg){
if(this.dialog.isShowing()){
this.dialog.disclose();
}
Toast.makeText(nastoragenaslist.this,“文件下载完成!”,
吐司。长度(短)。show();
}
}//结束下载任务
您有“打字错误”。您需要在publishProgress()
方法total变量中替换为downloadTotal
publishProgress((int) (downloadTotal * 100 / Long.parseLong(filesize)));
您的总计变量在循环之前仅被指定为零,并且从未更改为其他值。因此,您执行了division0/filesize
,它将始终为0