在内部存储中打开视频

时间:2017-02-22 15:36:48

标签: android android-videoview loopj

我使用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();

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

2 个答案:

答案 0 :(得分:0)

你不能这样做。 MediaPlayer使用的VideoView对象要求文件为world-readable。内部文件不是。使用类似Uri的{​​{1}}传递来源也是无用的:如果是这样,最后MediaPlayer将使用其setDataSource(String path)方法

同样检查this answer

答案 1 :(得分:0)

我找到了解决方案。它适用于内部存储。 我改为使用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();