【发布时间】:2011-02-07 19:28:36
【问题描述】:
我正在尝试从 Internet 下载大文件 (>20Mb)
private class DownloadTask extends AsyncTask<DatabaseInfo, Integer, String> {
private DatabaseInfo info;
protected String doInBackground(DatabaseInfo... dbInfo) {
int count;
info = dbInfo[0];
try {
URL url = new URL(dbInfo[0].dbPath);
InputStream input = new BufferedInputStream(url.openStream());
OutputStream output = new FileOutputStream("/sdcard/db.zip");
byte data[] = new byte[1024];
int total = 0;
while ((count = input.read(data)) != -1) {
//output.write(data, 0, count);
total += count;
if (total % 10240 == 0) {
publishProgress(total);
}
}
output.flush();
output.close();
input.close();
}
catch (Exception e) {
Log.e("err", e.getMessage());
}
return null;
}
protected void onProgressUpdate(Integer... total) {
int perc = (int) ((float) total[0] / (float) info.dbZipSize * 100);
mProgressDialog.setProgress(perc);
}
protected void onPostExecute(String s) {
dismissDialog(DIALOG_PROGRESS);
Log.e("err", "finish!");
}
}
如果我取消注释行
//output.write(data, 0, count);
在 7-15% 下载进度条对话框消失后,我看到“完成!”在日志中。为什么?
【问题讨论】:
-
你是在设备还是模拟器上试过??如果您在模拟器中尝试过,则无法正常工作。因为你需要高系统配置。
-
模拟器。好的,我会在设备上试试
标签: android asynchronous download android-asynctask