2

More specific: I am trying to load a video from res/raw with jcodec's FrameGrab.

FrameGrab requires a SeekableBiteChannel, so a File will work.

How can I get a video file from assets as a File?

I cannot put the video on the sd-card or anything similar, I am developing for Android Wear.

EDIT:

String videoPath = "android.resource://" + getPackageName() + "/" + R.raw.hyperlapse2;
mVideoTestUri = Uri.parse(videoPath);
Log.d("VideoPlayer", "Video uri is " + mVideoTestUri);
File file = new File(videoPath);
Log.d("VideoPlayer", "Video file is " + file+", "+file.getName()+", "+file.getAbsolutePath()+", "+file.length());
josedlujan
  • 5,357
  • 2
  • 27
  • 49
A. Steenbergen
  • 3,360
  • 4
  • 34
  • 51
  • possible duplicate of [How to reference a File in raw folder in Android](http://stackoverflow.com/questions/9752417/how-to-reference-a-file-in-raw-folder-in-android) – Semyon Danilov Feb 06 '15 at 15:38
  • Its not a duplicate, that question handles reading text files, I am reading a video – A. Steenbergen Feb 06 '15 at 15:49

2 Answers2

4

Finally I made it work. I don't know if this is something Android Wear specific or a bug, but it turns out that

String path = "android.resource://" + getPackageName() + "/" + R.raw.video_file;
File file = new File(path);

does not give access to the file on Android Wear devices.

Instead one has to convert the file into a temp file first:

InputStream ins = MainActivityBackup.this.getResources().openRawResource (R.raw.hyperlapse2);
File tmpFile = null;
OutputStream output;

try {
    tmpFile = File.createTempFile("video","mov");
    output = new FileOutputStream(tmpFile);

    final byte[] buffer = new byte[102400];
    int read;

    while ((read = ins.read(buffer)) != -1) {
        output.write(buffer, 0, read);
    }
    output.flush();
    output.close();
    ins.close();
} catch (IOException e) {
    e.printStackTrace();
}

And then it can be loaded into a videoView

mVideoView.setVideoPath(tmpFile.getPath());

Provided you are using your own video decoder or a library like ffmpeg or vitamio, since Android Wear does not support native video playback yet.

A. Steenbergen
  • 3,360
  • 4
  • 34
  • 51
0

Place the video in the resource directory under a folder called raw.

String path = "android.resource://" + getPackageName() + "/" + R.raw.video_file;
File file = new File(path);

VideoView view = (VideoView)findViewById(R.id.videoView);
view.setVideoURI(Uri.parse(path));
view.start();
Adam W
  • 972
  • 9
  • 18
  • maybe refresh the page, its there – A. Steenbergen Feb 06 '15 at 15:37
  • I tested the same code and even though the file length is incorrect the file plays correctly in a videoview. – Adam W Feb 06 '15 at 15:48
  • Did you get a file length of 0 as well or some different length alltogether? – A. Steenbergen Feb 06 '15 at 15:48
  • My file length returned 0. The file name returned as a random int which was assigned in resource creation. – Adam W Feb 06 '15 at 15:52
  • It's weird, I try to read it with jcodec but it keeps giving me a java.io.FileNotFoundException: /2130968577: open failed: ENOENT (No such file or directory) – A. Steenbergen Feb 06 '15 at 15:53
  • Just for the sake of testing the above code, I would add a VideoView to your activity and see if the video plays. I'll amend by above code. – Adam W Feb 06 '15 at 15:59
  • Its not possible to play the video natively on Android Wear, because it doesnt support video, only audio, thats why I need jcodec. – A. Steenbergen Feb 06 '15 at 16:02
  • Ah i see. My best guess then is that the video isn't supported by the codec. The above code does work in standard android. That's probably all the help I am going to be. Good Luck. – Adam W Feb 06 '15 at 16:09
  • I made it work with some workarounds, I posted the answer below. – A. Steenbergen Feb 06 '15 at 18:54