Kir*_*dev 3 android inputstream android-asynctask
我正在使用asynctask来下载文件.它正常工作,直到我关闭我的Android的wifi连接(没有其他互联网连接),仍然没有更改下载对话框.当我通过日志检查时,我发现输入流的函数read()是不停止的.那么如何检查这种情况呢?这是我的代码:
URL url = new URL(this.url);
URLConnection connection = url.openConnection();
connection.setReadTimeout(1000);
connection.connect();
// this will be useful so that you can show a typical 0-100% progress bar
int fileLength = connection.getContentLength();
fileName = "temp.zip";
// download the file
InputStream input = new BufferedInputStream(connection.getInputStream());
OutputStream output = new FileOutputStream(path+fileName);
byte buffer[] = new byte[1024000];
long total = 0;
int count;
Log.v("test download:","download in background");
while (((count = input.read(buffer)) != -1)) {
Log.v("test download:","read:"+count);
total += count;
publishProgress((int) (total * 100 / fileLength - 1));
output.write(buffer, 0, count);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1893 次 |
| 最近记录: |