I want to download a video file (extension 3gp) from a certain http address. My application starts downloading, but it stops after downloading 9216 bytes. The file length is 1734741. I tried to increase the heap (because I got some warning about increasing the heap size) by including the following line in the Manifest file:
android:largeHeap="true"
It does not help. Here is the fragment of the java code that deals with downloading:
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;
}
The application uses a Progress Bar to show the progress of the download. Because the file length is quite big (1734741 bytes), I did logarithmic calculations. Also, I have several Log.v messages to see what's going on, I hope this does not hamper understanding of the code. Anyway, my question refers only to the incomplete downloading of the 3gp file.
What can I do to download the video file? Any idea? Thanks in advance for the help.