Reputation: 3954
I am Trying to show Progress when media is downloading. song is properly downloaded and stored now I want to show how much Downloaded in percentage .
so can any one help me. This is my Code :-
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();
}
}
**All the thing are working well.... just I want to show percentage of remaining like 99%,98% remains...like wise **
Upvotes: 0
Views: 96
Reputation: 26
just add this code :
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]));
}
Upvotes: 1
Reputation: 29285
Simply replace
publishProgress("" + (int) ((total * 100) / lenghtOfFile));
with
publishProgress("" + (int) (((lenghtOfFile - total) * 100) / lenghtOfFile));
The former one grows from 0 to 100, but the latter one grows from 100 to 0.
Upvotes: 1