0

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.

Monica
  • 389
  • 1
  • 7
  • 19
  • Reading logic is, in general, done right. I wonder why `SystemClock.sleep(1000);` ? Not sure if it's actual problem here. – Volodymyr Lykhonis May 24 '13 at 00:16
  • I added it because of a warning that says: – Monica May 24 '13 at 00:58
  • 05-24 00:56:43.034: I/Choreographer(26737): Skipped 31 frames! The application may be doing too much work on its main thread. – Monica May 24 '13 at 00:59
  • It's not related. It is not because of this, since doInBackground actually do in background but UI thread. – Volodymyr Lykhonis May 24 '13 at 01:00
  • I see, but it does not work even without it. – Monica May 24 '13 at 01:02
  • Can you guarantee that file size in bytes of the file on server not same as what you saved/received in the file? This code is not clean and can possibly have side-effect problems. – Volodymyr Lykhonis May 24 '13 at 01:04
  • I got the length of the video file in bytes from the system, that is from connection.getContentLength(). But I think that the possible issue you are pointing to may only have to do with the progress bar, not with the downloading of the video file. – Monica May 24 '13 at 01:10
  • I re-wrote the code so that it does only the downloading. Maybe this way one can give me an idea about what's wrong with it. I really need help. Can't get it to download the file :( – Monica May 24 '13 at 21:15
  • Help yourself, remove unnecessary code from here as well as get familiar with http://developer.android.com/reference/java/net/HttpURLConnection.html – Volodymyr Lykhonis May 24 '13 at 21:32

0 Answers0