【问题标题】:Playing video from app cache dir从应用缓存目录播放视频
【发布时间】:2011-08-12 02:54:53
【问题描述】:

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

注意:正在下载此视频。在调用 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,无论它是否有效,都会碰巧失败。

谁能解释这种行为?

【问题讨论】:

标签: android video video-streaming


【解决方案1】:

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

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);                       

}

【讨论】:

  • 绝对不是权限问题。文件写入并有时会播放,但随着缓存迅速增长过大,它们似乎被清除了。我没有对此进行验证,但这是唯一合乎逻辑的事情。
猜你喜欢
  • 2014-01-03
  • 1970-01-01
  • 1970-01-01
  • 2022-10-11
  • 2020-09-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多