我想从某个 http 地址下载视频文件(扩展名 3gp)。我的应用程序开始下载,但在下载 9216 字节后停止。文件长度为 1734741。我试图通过在清单文件中包含以下行来增加堆(因为我收到了一些关于增加堆大小的警告):
机器人:大堆=“真”
它没有帮助。这是处理下载的java代码片段:
protected String doInBackground(String... sUrl) {
byte[] data = new byte[8];
double total = 0;
int count = 0;
FileOutputStream fos = null;
BufferedOutputStream output = null;
try {
Log.v("Here","the address is " + sUrl[0]);
URL url= new URL(sUrl[0]);
URLConnection connection = url.openConnection();
connection.connect();
Log.v("File ", "length = " + connection.getContentLength());
// this will be useful so that you can show a typical 0-100% progress bar
double fileLength = (Math.log10((double)connection.getContentLength()));
Log.v("DoInBackground","file length = " + fileLength);
if (fileLength <= 0)
fileLength = (long)(Math.log10(1734741.0));
Log.v("DoInBackground","again file length = " + fileLength);
// download the file
BufferedInputStream input = new BufferedInputStream(connection.getInputStream());
Log.v("Input ","is " + input);
Log.v("Output ","is " + Environment.getExternalStorageDirectory().getPath());
File f = new File(Environment.getExternalStorageDirectory().getPath() + "/twokids.3gp");
f.setWritable(true,true);
try {
fos = new FileOutputStream(f);
} catch (FileNotFoundException fnfe) {
fnfe.getStackTrace();
} catch (SecurityException se) {
se.getStackTrace();
}
output = new BufferedOutputStream(fos); /////????????
int i = (int)(Math.pow(10,(Math.log10(total) + 2 - fileLength)));
while ((count = input.read(data)) != -1) {
Log.v("Count","= " + count);
total += count;
Log.v("Total " + total,"Count " + count);
// publishing the progress....
if ((int)(total * 100 / fileLength) == 0)
i = i + 1;
Log.v("Progress","Increment" + i);
/////////////task.publishProgress(i);
prgs.setProgress(i);
SystemClock.sleep(1000);
output.write(data, 0, count);
}
output.flush();
output.close();
input.close();
} catch (IOException ioe) {
ioe.getStackTrace();
}
catch (Exception e) {
e.getStackTrace();
}
return null;
}
该应用程序使用进度条来显示下载进度。因为文件长度很大(1734741字节),所以我做了对数计算。另外,我有几条 Log.v 消息来查看发生了什么,我希望这不会妨碍对代码的理解。无论如何,我的问题仅涉及3gp文件的不完整下载。
我该怎么做才能下载视频文件?任何想法?在此先感谢您的帮助。