Reputation: 141
I'm downloading a mp3 file from the internet and saving it into the internal storage. The progress of the download is shown in a ProgressDialog
. The ProgressDialog
displays the progress in percent which is fine. In the right it displays the progress as 1/100 .... I'd like it to display the current size of the file downloaded in MB / the total size of the file being downloaded in MB.
Like in the following picture. The grey text is the current text being displayed. I'd like it to be displayed like the text below it in red.
Here's the current code :
public class DownloadSKHindi extends AsyncTask<String, Integer, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog = new ProgressDialog(getActivity());
progressDialog.setMessage("Downloading");
progressDialog.setMax(100);
progressDialog.setCancelable(false);
progressDialog.setCanceledOnTouchOutside(false);
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.show();
}
@Override
protected String doInBackground(String... params) {
int count = 0;
try {
url = new URL(skURL[10]);
connection = url.openConnection();
connection.connect();
int lengthOfFile = connection.getContentLength();
InputStream input = new BufferedInputStream(url.openStream());
OutputStream output = getActivity().openFileOutput(file.getPath(), Context.MODE_PRIVATE);
byte data[] = new byte[1024];
long total = 0;
while ((count = input.read(data)) != -1) {
total += count;
publishProgress((int) (total * 100 / lengthOfFile));
output.write(data, 0, count);
}
output.flush();
output.close();
input.close();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
progressDialog.setProgress(values[0]);
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
progressDialog.dismiss();
}
}
Upvotes: 0
Views: 3507
Reputation: 1109
Assuming total
is how many bytes are downloaded, and lengthOfFile
is the size in bytes of how large the file is. You can do this:
progressDialog.setProgressNumberFormat((bytes2String(total)) + "/" + (bytes2String(lengthOfFile)));
Then using the function bytes2String ( Credit to coder )
private static double SPACE_KB = 1024;
private static double SPACE_MB = 1024 * SPACE_KB;
private static double SPACE_GB = 1024 * SPACE_MB;
private static double SPACE_TB = 1024 * SPACE_GB;
public static String bytes2String(long sizeInBytes) {
NumberFormat nf = new DecimalFormat();
nf.setMaximumFractionDigits(2);
try {
if ( sizeInBytes < SPACE_KB ) {
return nf.format(sizeInBytes) + " Byte(s)";
} else if ( sizeInBytes < SPACE_MB ) {
return nf.format(sizeInBytes/SPACE_KB) + " KB";
} else if ( sizeInBytes < SPACE_GB ) {
return nf.format(sizeInBytes/SPACE_MB) + " MB";
} else if ( sizeInBytes < SPACE_TB ) {
return nf.format(sizeInBytes/SPACE_GB) + " GB";
} else {
return nf.format(sizeInBytes/SPACE_TB) + " TB";
}
} catch (Exception e) {
return sizeInBytes + " Byte(s)";
}
}
Upvotes: 7
Reputation: 27211
Use TextView
that is appended after progressDialog inside XML file.
Consider TextView tv = findViewById();
Use this: publishProgress((int) (total * 100 / lengthOfFile), total ,lengthOfFile);
to push new data.
And to update the tv
use this:
@Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
progressDialog.setProgress(values[0]);
tv.setText((values[1]/(1024*1024)) + "" +(values[1]/(1024*1024)));
}
Upvotes: 1