【问题标题】:Open a video in the internal storage在内部存储中打开视频
【发布时间】:2017-02-22 15:36:48
【问题描述】:

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

也许这不是一条好路?还是我保存视频的方式? 我指定视频必须下载到内部存储中。没有外部存储。

【问题讨论】:

    标签: java android android-videoview loopj


    【解决方案1】:

    你不能这样做。 VideoView 使用的MediaPlayer 对象要求文件为world-readable。内部文件不是。用Urifile:/// 一样传递源也是没用的:如果是这样,最后MediaPlayer 将使用它的setDataSource(String path) 方法

    检查this answer 也在同一个参数上。

    【讨论】:

      【解决方案2】:

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

      【讨论】:

      • 我必须等待 3 个小时。对不起。
      • 好的。我没有意识到这一点。对不起。
      猜你喜欢
      • 2020-05-27
      • 2015-12-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多