1

我正在开发一个循环播放视频文件的应用程序。到目前为止,我只是安装设备并将视频文件复制到 SD 卡上,然后使用文件路径在我的 VideoView 上启动它。我正在尝试实现一种可以远程更新它播放的视频的方法,因此我已经开始在线存储我的视频。在应用程序中,我检查本地副本并下载它是否不存在,或者是否有更新的副本。我已经在两个不同的视频文件上测试了它,都是 .mp4s。下载其中一个第一次播放后,但在尝试重新开始循环时,它告诉我无法播放视频。另一个甚至不会第一次播放,它只是给我一个对话框,说视频无法播放。如果我通过 USB 电缆将这两个文件复制到 SD 卡上,这两个文件都可以在我的应用程序中正常工作。如果我退出我的应用程序并使用其他东西(保管箱)手动下载它们,它们就可以工作,但如果我从我的应用程序中下载它们就不行。这是我用来下载文件的代码:

public static void DownloadFromUrl(String fileName) {  //this is the downloader method
    try {
        URL url = new URL("http://dl.dropbox.com/u/myfile.mp4"); 
        File file = new File(PATH + fileName);
        long startTime = System.currentTimeMillis();
        Log.d(myTag, "download begining");
        Log.d(myTag, "download url:" + url);
        Log.d(myTag, "downloaded file name:" + fileName);


        /* Open a connection to that URL. */
        URLConnection ucon = url.openConnection();
        Log.i(myTag, "Opened Connection");

        /*
         * Define InputStreams to read from the URLConnection.
         */
        InputStream is = ucon.getInputStream();
        BufferedInputStream bis = new BufferedInputStream(is);
        Log.i(myTag, "Got InputStream and BufferedInputStream");
        FileOutputStream fos = new FileOutputStream(file);
        BufferedOutputStream bos =  new BufferedOutputStream(fos);
        Log.i(myTag, "Got FileOutputStream and BufferedOutputStream");
        /*
         * Read bytes to the Buffer until there is nothing more to read(-1).
         */

        int current = 0;
        Log.i(myTag, "About to write");
        while ((current = bis.read()) != -1) {
            bos.write(current);
        }



        fos.close();
        Log.d(myTag, "download ready in"
                + ((System.currentTimeMillis() - startTime))
                + " sec");
    } catch (IOException e) {
        Log.d(myTag, "Error: " + e);
    }
}

我知道这个片段中的保管箱 url 不正确我只为这篇文章更改了它,在我的应用程序中,url 正确指向了一个文件。创建文件时使用的变量 PATH 在我的代码中设置在此代码段之外。

此代码片段是否有可能损坏我的 mp4 文件?

4

1 回答 1

2

该方法以某种方式损坏了文件,我仍然不太确定如何,但我更改了其中的一部分,现在它已修复。

我现在正在使用这个:

        byte data[] = new byte[1024];
        long total = 0;
        int count;
        while ((count = bis.read(data)) != -1) {
            total += count;
            fos.write(data, 0, count);
        }

        fos.flush();
        fos.close();

而不是旧的while循环,它可以正常工作。

于 2011-03-15T19:53:50.583 回答