我正在使用异步任务从互联网下载一些文件。对于这个问题,我知道向用户展示到目前为止的进展是非常相关的。我下载文件的所有实现都非常成功,但唯一的问题是进度对话框,即使启动了下载命令,这也显示为0%。
我就是这样做的
// Show Dialog Box with Progress bar
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case progress_bar_type:
prgDialog = new ProgressDialog(this);
prgDialog.setMessage("Downloading Mp3 file. Please wait...");
prgDialog.setIndeterminate(false);
prgDialog.setMax(100);
prgDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
prgDialog.setCancelable(false);
prgDialog.show();
return prgDialog;
default:
return null;
}
}
// Async Task Class
class DownloadMusicfromInternet extends AsyncTask<String, String, String> {
// Show Progress bar before downloading Music
@Override
protected void onPreExecute() {
super.onPreExecute();
// Shows Progress Bar Dialog and then call doInBackground method
showDialog(progress_bar_type);
}
// Download Music File from Internet
@Override
protected String doInBackground(String... f_url) {
int count;
try {
URL url = new URL(f_url[0]);
URLConnection conection = url.openConnection();
conection.connect();
// Get Music file length
int lenghtOfFile = conection.getContentLength();
// input stream to read file - with 8k buffer
InputStream input = new BufferedInputStream(url.openStream(),10*1024);
// Output stream to write file in SD card
OutputStream output = new FileOutputStream(f_url[1]);
byte data[] = new byte[1024];
long total = 0;
while ((count = input.read(data)) != -1) {
total += count;
// Publish the progress which triggers onProgressUpdate method
publishProgress("" + (int) ((total * 100) / lenghtOfFile));
// Write data to file
output.write(data, 0, count);
}
// Flush output
output.flush();
// Close streams
output.close();
input.close();
} catch (Exception e) {
Log.e("Error: ", e.getMessage());
}
return null;
}
// While Downloading Music File
protected void onProgressUpdate(String... progress) {
// Set progress percentage
prgDialog.setProgress(Integer.parseInt(progress[0]));
}
// Once Music File is downloaded
@Override
protected void onPostExecute(String file_url) {
// Dismiss the dialog after the Music file was downloaded
dismissDialog(progress_bar_type);
Toast.makeText(getApplicationContext(), "Download complete, playing Music", Toast.LENGTH_LONG).show();
}
}
请根据下载时间的移动,如何使进度移动。谢谢你的帮助。
更新
我认为这个问题与所谓的重复问题不一样,我研究后的问题来自于int lenghtOfFile = conection.getContentLength();
。
进程不会移动,因为getContentLength()
总是返回-1,因为它无法从服务器获得文件大小。我见过很多这样的问题,但都没有答案。请问有出路吗?我很想知道。先谢
发布于 2016-02-21 00:39:26
正如您在评论中所说的,getContentLength()正在返回-1。这将导致您的进度更新始终是负面的,这意味着进度条永远不会前进。这就是为什么它没有像你期望的那样移动。
根据javadoc,当没有内容长度头或由于某种原因不能将其解析为数字时,getContentLength()返回-1。在这种情况下,您无法提供基于下载文件大小的进度表。
发布于 2016-02-21 08:21:53
如果进度条设置为不确定,则不需要调用prgDialog.setProgress(Integer.parseInt(progress[0]));
。这可能就是为什么没有显示出进步的原因。
下载开始时调用prgDialog.setIndeterminate(true)
,然后在下载结束时调用prgDialog.setIndeterminate(false)
。在使用不确定进度条时,请避免调用setProgress
。
编辑:矩形:从官方文件调用,如果条形图处于不确定模式,setProgress
什么也不做。
此外,您还说要将不确定值设置为true,但是在onCreateDialog
方法中我看到了prgDialog.setIndeterminate(false);
。
https://stackoverflow.com/questions/35531302
复制