我正在尝试在下载媒体时显示进度。 歌曲已正确下载并存储现在我想显示多少已下载 百分比。
任何人都可以帮助我。 这是我的代码: -
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%仍然......就像明智**
答案 0 :(得分:1)
简单地替换
publishProgress("" + (int) ((total * 100) / lenghtOfFile));
与
publishProgress("" + (int) (((lenghtOfFile - total) * 100) / lenghtOfFile));
前者从0增加到100,但后者从100增长到0。
答案 1 :(得分:1)
只需添加此代码:
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]));
}