从Internet下载3gp视频文件

时间:2013-05-24 00:09:24

标签: android video emulation 3gp

我想从某个http地址下载视频文件(扩展名为3gp)。我的应用程序开始下载,但下载9216字节后停止。文件长度是1734741.我尝试通过在清单文件中包含以下行来增加堆(因为我收到了一些关于增加堆大小的警告):

机器人:largeHeap = “真”

没有用。以下是处理下载的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文件的不完整下载。

我该怎么做才能下载视频文件?任何的想法?在此先感谢您的帮助。

0 个答案:

没有答案
相关问题