gpt4 book ai didi

java - 如何显示下载进度?

转载 作者:行者123 更新时间:2023-12-02 03:54:21 27 4
gpt4 key购买 nike

我试图在下载媒体时显示进度。歌曲现在已正确下载并存储我想显示已下载的数量以百分比表示。

有人可以帮助我吗?这是我的代码:-

class DownloadFile extends AsyncTask<String, String, String> {
@Override
protected String doInBackground(String... url) {

//............ declration and all
if (fsize != lenghtOfFile) {
int filesize = f.getAbsolutePath().length();
if (filesize != lenghtOfFile) {
InputStream input = new BufferedInputStream(url1.openStream());
OutputStream output = new FileOutputStream(f);
byte data[] = new byte[1024];
long total = 0;
while ((count = input.read(data)) != -1) {
total += count;
publishProgress("" + (int) ((total * 100) / lenghtOfFile));
// pDialog.setMessage( String.valueOf((int) ((total * 100) / lenghtOfFile)) +"Downloaded.");
output.write(data, 0, count);
}
output.flush();
output.close();
input.close();
}
//...... other code
}

@Override
protected void onProgressUpdate(String... values) {
super.onProgressUpdate(values);
}

@Override
protected void onPostExecute(String result) {
//on post over
pDialog.dismiss();
}
@Override
protected void onPreExecute() {
//set Dialog
pDialog = new ProgressDialog(getActivity());
pDialog.setMessage("Please wait song is Downloading...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
}

**一切都运转良好......只是我想显示剩余的百分比,例如 99%,98% 的剩余......同样明智 **

最佳答案

只需添加此代码:

     protected void onPreExecute() {
pDialog = new ProgressDialog(getActivity());
pDialog.setMessage("Downloading...");
pDialog.setIndeterminate(false);
pDialog.setMax(100);
pDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
pDialog.setCancelable(true);
pDialog.show();
}
@Override
protected void onProgressUpdate(String... values) {
super.onProgressUpdate(values);
pDialog.setProgress(Integer.parseInt(values[0]));
}

关于java - 如何显示下载进度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35626586/

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