1.如下 我的代码我已经更新了。
2. 每当我尝试从服务器下载数据时,我都在使用进度计数对话框,但是当下载开始时它显示计数百分比。
3. 但问题是计数最多只显示 30% 当百分比达到 30% 时对话框将关闭(这意味着所有数据已成功下载)
4. 但我需要显示对话框直到 100% 完成然后它应该被关闭.. 无论服务器有什么文件它大约有 5 到 6 个文件。
@Override
protected void onPreExecute() {
this.progressDialog = new ProgressDialog(BaseApplication.getInstance().getCurrentActivity());
this.progressDialog.setMessage("Downloading Please wait.");
this.progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
this.progressDialog.setMax(100);
this.progressDialog.setCancelable(false);
this.progressDialog.show();
}
@Override
protected Boolean doInBackground(String... strings) {
// Here is my downloading code but i removed
DownloadData("" , "" , ""); //stuff
}
return true;
}
@Override
protected void onProgressUpdate(String... values) {
this.progressDialog.incrementProgressBy(5);
}
protected void onPostExecute(Boolean result) {
this.progressDialog.dismiss();
}
private void downloadData(String Folder, String Name, String URL) {
Log.i(TAG, "Coming to this downloadBookDetails ");
// int url1 = Integer.parseInt(pDownloadURL);
int len;
try {
// int urlsixe = pDownloadURL.length();
// Log.i(TAG, "URL lenght" + urlsixe);
URL url = new URL(URL);
Log.i(TAG, "pDownload URL" + url);
URLConnection ucon = url.openConnection();
ucon.setReadTimeout(5000);
ucon.setConnectTimeout(10000);
ucon.connect();
int lenghtOfFile = ucon.getContentLength();
Log.i(TAG, "lenght of file" + lenghtOfFile);
InputStream is = ucon.getInputStream();
Log.i(TAG, " InputStream" + is);
BufferedInputStream inStream = new BufferedInputStream(is, 1024 * 5);
Log.i(TAG, " BufferedInputStream" + inStream);
File directory = new File(pMainFolder, pFileName);
Log.i(TAG, "File Name dir" + directory);
FileOutputStream outStream = new FileOutputStream(directory);
byte[] buff = new byte[5 * 1024];
long total = 0;
while ((len = inStream.read(buff)) != -1) {
total += len;
// publishProgress("" + (int) ((total * 100) / urlsixe));
// Log.i(TAG, "Progress: Bar with Count " + (int) ((total * 100) / lenghtOfFile));
outStream.write(buff, 0, len);
}
publishProgress();
outStream.flush();
outStream.close();
inStream.close();
} catch (Exception e) {
//Add Network Error.
Log.i(TAG, "Download Error Exception " + e.getMessage());
e.printStackTrace();
}
}
最佳答案
在您的 Asynctask onPostExecute 中检查是否达到最大值。如果达到则关闭对话框
protected void onPostExecute(Boolean result) {
if (barProgressDialog.getProgress() == barProgressDialog.getMax()) {
this.progressDialog.dismiss();
}
}
关于java - 进度计数未显示 100%,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53930304/