在进度条中显示以KB格式下载的文件

时间:2014-12-02 10:26:56

标签: android

我想创建一个进度条。 我还想显示文件下载/总文件大小。例如12.23kb / 10 mb。 我是否需要在进度条周围使用文本框,或者进度条是否提供内置方法。

class showdownload{
ProgressBar progressbar = null;
setProgressBarProgress(String... progress)
{

  String kb = progress[0];
  String mb = progress[1];
  String percentage = Integer.toInt(progress[2]);
  progressbar.setProgress(percentage );

}
}

2 个答案:

答案 0 :(得分:0)

您可以尝试这种方式:

@Override
protected String doInBackground(String... aurl) {
    int count;

try {

URL url = new URL(aurl[0]);
URLConnection conexion = url.openConnection();
conexion.connect();

int lenghtOfFile = conexion.getContentLength();
Log.d("ANDRO_ASYNC", "Lenght of file: " + lenghtOfFile);

InputStream input = new BufferedInputStream(url.openStream());
OutputStream output = new FileOutputStream("/sdcard/some_photo_from_gdansk_poland.jpg");

byte data[] = new byte[1024];

long total = 0;

    while ((count = input.read(data)) != -1) {
        total += count;
        publishProgress(""+(int)((total*100)/lenghtOfFile));
        output.write(data, 0, count);
    }

    output.flush();
    output.close();
    input.close();
} catch (Exception e) {}
return null;

}
protected void onProgressUpdate(String... progress) {
     Log.d("ANDRO_ASYNC",progress[0]);
     mProgressDialog.setProgress(Integer.parseInt(progress[0]));
}

<强>输出

enter image description here

答案 1 :(得分:0)

您可以将进度条的范围设置为下载大小(例如10.000 Kb),然后以Kb为单位设置进度。它应该准确显示你想要的东西。

我建议使用ProgressDialog类。