从应用程序缓存目录播放视频

发布于 2024-11-29 02:39:17 字数 490 浏览 0 评论 0原文

谁能解释为什么从我的应用程序缓存目录下载/播放视频不起作用,但从我的 SD卡下载/播放相同的视频却起作用?

注:该视频正在下载中。我在调用 VideoView.setVideoPath(...) 之前保存到内存。

// Works
File file = new File(Environment.getExternalStorageDirectory(), "vid-test.3gp");

// Does not work
File file = new File(getCacheDir(), "vid-test.3gp");

在每种情况下,相关文件确实存在。

如果我尝试调用 VideoView.setVideoURI(...) 并将视频“流式传输”到我的 VideoView,则无论是否有效,都可能会失败。

谁能解释这种行为?

Can anyone explain why downloading/playing a video from my applications cache directory does not work, but downloading/playing the same video from my sdcard does work?

Note: this video is being downloaded. I am saving to memory before calling VideoView.setVideoPath(...).

// Works
File file = new File(Environment.getExternalStorageDirectory(), "vid-test.3gp");

// Does not work
File file = new File(getCacheDir(), "vid-test.3gp");

In each case the file in question does exist.

If I attempt to call VideoView.setVideoURI(...) and "stream" the video to my VideoView, it is hit and miss whether or not it will work.

Can anyone explain this behavior?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蓝眸 2024-12-06 02:39:17

这可能是一个权限问题。这是一个工作片段:

InputStream in = connection.getInputStream();

File file = new File(getApplicationContext().getCacheDir() ,fileName);

if(!file.exists()){
    file.setReadable(true);

    file.createNewFile();


    if (file.canWrite()){

        FileOutputStream out = new FileOutputStream(file);   

        byte[] buffer = new byte[1024];
        int len1 = 0;
        while ( (len1 = in.read(buffer)) > 0 ) {
            out.write(buffer,0, len1);
        }

        out.close();
    }

    in.close();

    Runtime.getRuntime().exec("chmod 755 "+getCacheDir() +"/"+ fileName);                       

}

It's probably a permission issue. Here is a working snipped:

InputStream in = connection.getInputStream();

File file = new File(getApplicationContext().getCacheDir() ,fileName);

if(!file.exists()){
    file.setReadable(true);

    file.createNewFile();


    if (file.canWrite()){

        FileOutputStream out = new FileOutputStream(file);   

        byte[] buffer = new byte[1024];
        int len1 = 0;
        while ( (len1 = in.read(buffer)) > 0 ) {
            out.write(buffer,0, len1);
        }

        out.close();
    }

    in.close();

    Runtime.getRuntime().exec("chmod 755 "+getCacheDir() +"/"+ fileName);                       

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文