打开内部存储中的视频

Open a video in the internal storage

我使用 WS 在我的应用程序中下载视频。 我要打开下载的视频后

问题是当我想打开视频时出现此错误:

VideoView: Unable to open content: /data/user/0/code.package/files/diapos/1.mp4
java.io.IOException: setDataSource failed.

这是下载功能:

MyRestClient.get("/diapos/1", null, new BinaryHttpResponseHandler() {
  @Override
  public void onSuccess(int statusCode, Header[] headers, byte[] binaryData) {
    InputStream input = new ByteArrayInputStream(binaryData);
    try {
        OutputStream output = new FileOutputStream("/diapos/1.mp4");
        byte data[] = new byte[4096];
        int count;
        while ((count = input.read(data)) != -1) {
        output.write(data, 0, count);
        }
     } catch (IOException e) { e.printStackTrace(); }
}

我是这样播放视频的:

final VideoView video = (VideoView) getActivity().findViewById(R.id.video);
String path = getActivity().getFilesDir().getAbsolutePath() + "/diapos/1.mp4";
video.setVideoURI(Uri.parse(path));
video.start();

也许这条路不好?或者我保存视频的方式? 我指定比必须在内部存储中下载视频。没有外部存储。

你不能这样做。 VideoView使用的MediaPlayer对象要求文件为world-readable. Internal files aren't. It is also useless to pass the source with a Uri like file:///: if so, at the end the MediaPlayer will use its setDataSource(String path)方法

检查 同样的参数。

我找到了解决方案。它与内部存储一起工作。 我改用 FileAsyncHttpResponseHandler。

这是我调用 WS 的地方:

File file = getApplicationContext().getFileStreamPath("movie.mp4");

        Client.TESTVIDEOget(null, new FileAsyncHttpResponseHandler(file) {

            @Override public void onSuccess(int statusCode, Header[] headers, File file) {
                Log.d("debug", "success : " + file.getAbsolutePath() + "\n size : " + file.length());
            }

            @Override public void onFailure(int statusCode, Header[] headers, Throwable throwable, File file) {
                Log.d("debug", "fail : " + file.getAbsolutePath());

            }
        });

这是我放电影的地方:

final VideoView video = (VideoView) getActivity().findViewById(R.id.video);

String path = getActivity().getFilesDir().getAbsolutePath() + "/movie.mp4";
video.setVideoURI(Uri.parse(path));
video.start();