【发布时间】:2018-03-03 10:51:17
【问题描述】:
我想下载带有更新进度条而不是 ProgressDialog 的文件。我使用 AsyncTask 下载文件。在 onProgressUpdate() 方法中,我想更新进度条进度但没有成功。在日志中,进度是打印但进度条不更新。我的代码在适配器中。
class DownloadFileFromURL extends AsyncTask<String, String, String>
{
@Override
protected void onPreExecute()
{
super.onPreExecute();
}
@Override
protected String doInBackground(String... f_url)
{
int count;
try
{
URL url = new URL(f_url[0]);
URLConnection conection = url.openConnection();
conection.connect();
int lenghtOfFile = conection.getContentLength();
InputStream input = new BufferedInputStream(url.openStream(), 8192);
OutputStream output = new FileOutputStream("/sdcard/downloadedfile.mp3");
byte data[] = new byte[1024];
long total = 0;
while ((count = input.read(data)) != -1)
{
total += count;
// publishing the progress....
// After this onProgressUpdate will be called
publishProgress(""+(int)((total*100)/lenghtOfFile));
// writing data to file
output.write(data, 0, count);
}
// flushing output
output.flush();
// closing streams
output.close();
input.close();
}
catch (Exception e)
{
Log.e("Error: ", e.getMessage());
}
return null;
}
protected void onProgressUpdate(String... progress)
{
ViewHolder.progressbar.setProgress(Integer.parseInt(progress[0]));
}
}
请帮帮我。
【问题讨论】:
-
progressbar.setIndeterminate(false); progressbar.setMax(100);添加这两行
标签: android android-progressbar