Get the FULL version

How to get Android local files URI

How to get Android local files URI thumbnail

When programming applications for Android that requires the playback of audio or video files, sometimes, there’s the need to obtain the URI of those media files instead of using a String for the absolute path. But what is a URI? A URI (Uniform Resource Identifier) is an address to an local or internet resource. It’s more like a standardized path syntax that allows pointing to a specific resource that’s available over the internet, however we are going to use it to point it to a local resource.

A URI is specially useful, when using the VideoView class to load a video located on the res folder or in the SD card. Passing the video file to the VideoView as a String won’t even work on an emulated Android device. This way, we need to get the URI of the file.

'res' folder or SD Image

This can be used to get any file URI, at the 'res' folder or inside the SD Card.

To better explain how to obtain a file’s URI, let’s assume that we are trying to get the URI of a video named intro.3gp which is in the /res/raw folder. Here’s the code to get the URI:

Uri introURI;
introURI = Uri.parse("android.resource://your.app.package/" + R.raw.intro);

Now let’s assume the same intro.3gp is at the root of the SD card. The code to correctly get the file’s URI would be:

String introURI;
introURI = Uri.parse("file:///sdcard/intro.3gp");

And that’s it! This method can also be used to obtain any file URI. Expanding a little bit the previous example, to correctly play the video file while using the emulator, instead of using setVideoPath(String path) like:

String introURI = "file:///sdcard/intro.3gp";
VideoView videoView;

//...Omitted videoView initialization...

videoView.setVideoPath(path);

Obtain the video URI, as show on the previous examples and use the setVideoURI(URI uri) method, like this:

Uri introURI,
//obtain the URI of the video file from the 'res' folder
introURI = Uri.parse("android.resource://your.app.package/" + R.raw.intro);
//or get it the URI from a the video file at the SD card
//introURI = Uri.parse("android.resource://your.app.package/" + R.raw.intro);

VideoView videoView;

//...Omitted videoView initialization...

videoView.setURI(introURI);

10 Comments to “How to get Android local files URI”

  1. Shubham says:

    Hi I try your approach to uri of file..but it show
    android.resource://com.sample.openni/4578125
    means some digit at place of file name…pls suggest me possible way to get exact path of any file from local folder.

    thanks

  2. Juan Ci says:

    You define introUri as String.
    String introURI;

    Don’t you get an error with this assignement?
    introURI = Uri.parse(“file:///sdcard/intro.3gp”);

  3. soft_developer says:

    Hi, I would like to know how can I know the Mime type of the video media, after I but it in a videoView, indeed, I you to convert it into Base 64,

    Any idea / help?

    Thanks

  4. balwinder says:

    how to get the path of images of drawable folders in android

  5. Hi there to every body, it’s my first pay a quick
    visit of this webpage; this weblog includes amazing and genuinely fine stuff
    in favor of visitors.

  6. crib says:

    Valuable info. Fortunate me I found your site by accident, and I’m surprised why this coincidence didn’t took place
    in advance! I bookmarked it.

  7. painless says:

    I’ve been exploring for a bit for any high-quality
    articles or weblog posts in this sort of house .
    Exploring in Yahoo I finally stumbled upon this web site.
    Studying this info So i am happy to express that I have an incredibly just right
    uncanny feeling I discovered just what I needed. I most surely will
    make certain to do not disregard this site and provides it a look regularly.

  8. 18hoki says:

    Bagus dan bermanfaat. Admin bales dong, please hehe.

  9. mahabet.co says:

    Thank you for any other great article. The place else
    could anyone get that type of information in such an ideal way of writing?

    I have a presentation next week, and I’m at
    the look for such information.

Leave a Reply to DimasTheDriver

Post Comments RSS