Android Studio: Read .mp4 from res/raw/ directory frame by frame

2.3k Views Asked by At

For my app I need to load the individual images of a video file located in my res/raw/ directory into a list/array of images. From this list/array of images I need to pick some indecies, which will be stored into a new video video file also located in the res/raw/directory.

The problem is that I can not get the path to my video. If i try to use:

File f = new Flie("app/res/raw/test.mp4");

I get the error: the file can not be found.

I tried using Uri like:

String videopath ="android.resource://" + getPackageName()+ R.raw.test; File f = new Flie(videopath.toString());

But this does not work either.

Here is a pseudo code how I would need it:

List<Picture> video = new ArrayList<Picture>();
File file = new file("path_to_file/test.mp4");
FrameGrab grab = FrameGrab.createFrameGrab(NIOUtils.readableChannel(file));
Picture picture;

while (null != (picture = grab.getNativeFrame())) {
    video.add(picture);
}


List<picture> video_new = new ArrayList<picture>();
int[] idx = {1,2,4,6,8 ...}

for(int i= 0; i<idx.length; i++){
    picture= video.get(idx[i]);
    video_new.add(picture);
}

//stores the new video into the same path but with a different name
storefile("path_to_file/test_new.mp4", video_new);

1

There are 1 best solutions below

5
On

It looks like you may be missing a slash in your file path.

Try changing:

String videopath ="android.resource://" + getPackageName()+ R.raw.test

To:

String videopath ="android.resource://" + getPackageName()+ "/" + R.raw.test

Using this to access and play a video would look like:

VideoView view = (VideoView)findViewById(R.id.videoView);
String videopath ="android.resource://" + getPackageName()+ "/" + R.raw.test
view.setVideoURI(Uri.parse(videopath));
view.start();

Note

To extract frames from a media file (e.g. a video) Android provides the MediaMetadataRetriever class