gpt4 book ai didi

Android AsyncTask publishProgress 不更新对话框

转载 作者:太空宇宙 更新时间:2023-11-03 12:37:20 24 4
gpt4 key购买 nike

我正在尝试更新进度对话框以增加 1-100% 之类的值。但是,没有任何东西会增加。它总是只显示 0。尽管如此,我能够在我的 Log Cat 中看到打印出的数字,并且一切都在 100 之后结束。

有人看到我错过了什么吗?在此先感谢您的帮助。

 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

最佳答案

您有“打字错误”错误。您需要将 publishProgress() 方法中的 total 变量替换为 downloadTotal

publishProgress((int) (downloadTotal * 100 / Long.parseLong(filesize)));

您的total 变量在循环之前仅被分配为零,并且永远不会更改为其他值。因此,您执行的除法 0/filesize 始终为 0。

关于Android AsyncTask publishProgress 不更新对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15793197/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com