0
我正在使用此代码段从服务器下载mp4文件。通常文件得到正确下载, 但有时我的下载过程自动停止下载。进度条停止递增。是否有任何停止下载的内核进程。在Android应用程序中从服务器下载文件时出现问题
//+++++++++= FUnction Call ++++++++++++++++
DOWNLOAD_FILE_NAME = "demo.mp4";
grabURL("mp4 file server url");
//+++++++++++++++++++++++++++++++++++++++++
public void grabURL(String url) {
new GrabURL().execute(url);
}
private class GrabURL extends AsyncTask<String, Void, Void> {
private final HttpClient Client = new DefaultHttpClient();
private String Content;
private String Error = null;
private ProgressDialog Dialog = new ProgressDialog(SlideShow.this);
protected void onPreExecute() {
showDialog(DIALOG_PROGRESS);
mProgressDialog.setProgress(0);
}
protected Void doInBackground(String... url) {
int count;
try {
URL url2 = new URL(url[0]);
URLConnection conexion = url2.openConnection();
conexion.setUseCaches(true);
conexion.connect();
// this will be useful so that you can show a tipical 0-100% progress bar
int lenghtOfFile = conexion.getContentLength();
mProgressDialog.setMax(lenghtOfFile);
// downlod the file
InputStream input = new BufferedInputStream(url2.openStream());
OutputStream output = new FileOutputStream(DOWNLOAD_FILE_NAME);
byte data[] = new byte[1024];
while ((count = input.read(data)) != -1) {
mProgressDialog.incrementProgressBy(data.length);
output.write(data, 0, count);
}
output.close();
input.close();
} catch (Exception e) {}
return null;
}