下载媒体时,我正在尝试显示进度。现在已正确下载并存储了这首歌。我要显示百分比的下载量。
谁能帮我。这是我的代码:
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]));
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句