How to get Android local files URI
2016-09-22 17:09
183 查看
Posted by Dimitri | Oct 28th, 2010 | Filed under Programming
![](http://www.41post.com/uploads/2010/09/androidicon.gif)
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.
![](http://www.41post.com/uploads/2010/10/res_sd.jpg)
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 usingsetVideoPath(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);
![](http://www.41post.com/uploads/2010/09/androidicon.gif)
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.
![](http://www.41post.com/uploads/2010/10/res_sd.jpg)
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 usingsetVideoPath(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);
相关文章推荐
- How to bind the local files to TFS Server
- How to get the android resolution
- How to Get an Android Device Image?
- Android: How to get Address from geolocation using Geocoder
- How To Get Log, Trace Files In OA Framework Pages And Concurrent Request Programs
- How to extract .img files built from AOSP(Android Open Source Project)?
- Kernel.org hacked – how to get Android repo?
- How to get Linux Kernel Source Code of Android?
- How To Get Log, Trace Files In OA Framework Pages And Concurrent Request Programs
- How to get Android Thread ID?
- How to get files to show up in the Process Monitor/Report Repository
- How to get camped network type name - Android应用如何获得Phone当前注册的网络类型
- how to get toolchain from android source -make windows sdk
- Android: How to get Address from geolocation using Geocoder
- How to get local machine date format.如何获取本机时间格式
- how to get the android extra support jar
- How to get the identity hash codes for files
- Android:How to load files from assets folder?
- How to get Android Phone ServiceState - APK应用如何获得Android Phone的ServiceState,两种方法
- How to get local machine name and IP address?