javaandroidandroid-videoviewloopj

Open a video in the internal storage


I use a WS to download videos in my app. After I want to open the downloaded video.

The problem is when I want to open the video I have this error :

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

This is the download function :

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

This is how I play the video :

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

Maybe it's not the good path ? Or the way I save the video ? I specify than the video have to be downloaded in the internal storage. There is not external storage.


Solution

  • I found the solution. It work with the Internal Storage. I use a FileAsyncHttpResponseHandler instead.

    This is where I do my call of 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());
    
                }
            });
    

    This is where I play the movie :

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