Question

I am trying to get a URI to a resource in my res/raw/ directory. The goal is to give this URI to a VideoView, but this has been problematic, and I seem to be unable to open the file with test code.

I have the following test code:

String uri = "android.resource://com.my.package/" + R.raw.sample_video;

File inputFile = new File(uri);
try {
    InputStream is = new FileInputStream(inputFile);
    byte[] bytes = new byte[50];
    is.read(bytes);
    Log.d("test", new String(bytes));
} catch(IOException e) {

}

The new FileInputStream line throws a FileNotFoundException regardless of what variation i try on the URI, but every piece of evidence I see seems to agree that this is the correct form.

For reasons relating to the architecture of the project, the Resource methods that return an InputStream directly aren't an option here, so the URI is the only option that I can see.

What is going wrong? Am I mistaken in how to specify the URI for this file? Is this test code not representative of whether or not the VideoView will be able to read the file? If not, what is? Does this test code work for you (which would indicate that something must be wrong with my project configuration)?

No correct solution

OTHER TIPS

To get the URI of sample_video in raw folder :

  Uri videoUri = Uri.parse("android.resource://" + getPackageName() + "/" 
+ R.raw.sample_video); //do not add any extension

To play the video :

videocontainer.setVideoURI(Uri.parse(videoUri));
videocontainer.start();

where videocontainer of type videoview.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top