从应用程序缓存目录播放视频 [英] Playing video from app cache dir

查看:133
本文介绍了从应用程序缓存目录播放视频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以解释为什么下载/从我的应用程序缓存目录播放视频不工作,但是,下载/播放从我的SD卡相同的视频确实工作?

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?

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

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.

如果我试图调用 VideoView.setVideoURI(...)和流的视频,我的 VideoView ,被击中而错过它是否会奏效。

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?

推荐答案

这可能是一个权限问题。下面是一个工作剪断:

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

}

这篇关于从应用程序缓存目录播放视频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆