问题
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?
回答1:
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);
}
来源:https://stackoverflow.com/questions/7035133/playing-video-from-app-cache-dir