以MB显示文件下载进度

时间:2017-03-28 20:46:40

标签: android progressdialog

我从互联网上下载mp3文件并将其保存到内部存储中。下载进度显示在ProgressDialog中。 ProgressDialog以百分比显示进度,这很好。在右侧,它显示进度为1/100 ....我希望显示以MB为单位下载的文件的当前大小/以MB为单位下载的文件的总大小。

如下图所示。灰色文本是显示的当前文本。我希望它显示为红色下面的文字。

enter image description here

这是当前的代码:

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();
    }
}

2 个答案:

答案 0 :(得分:3)

假设total是下载的字节数,lengthOfFile是文件大小的字节大小。你可以这样做:

progressDialog.setProgressNumberFormat((bytes2String(total)) + "/" + (bytes2String(lengthOfFile)));

然后使用函数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)";
    }

}

Source

答案 1 :(得分:1)

使用在XML文件中的progressDialog之后附加的TextView。 考虑TextView tv = findViewById();

使用此:publishProgress((int) (total * 100 / lengthOfFile), total ,lengthOfFile);来推送新数据。

要更新tv,请使用此代码:

 @Override
    protected void onProgressUpdate(Integer... values) {
        super.onProgressUpdate(values);
        progressDialog.setProgress(values[0]);
tv.setText((values[1]/(1024*1024)) + "" +(values[1]/(1024*1024)));
    }