Showing posts with label resources. Show all posts
Showing posts with label resources. Show all posts

Monday, August 17, 2009

Referring to Android Resources Using URIs

In addition to loading Android resources using the Resource manager, you can also reference resources using a specially-formatted URI. Resource URIs can be referenced by resource type/name or by resource identifier. This can be especially useful if you are using a control like a VideoView which takes either a file path or a URI for the video source.

For example, let's say we have a VideoView and we want to load a resource from the raw resources called myvideo.3gp. We could construct a URI in two ways:

Using the resource id, the format is:

"android.resource://[package]/[res id]"

Uri path = Uri.parse("android.resource://com.androidbook.samplevideo/" + R.raw.myvideo);

or, using the resource subdirectory (type) and resource name (filename without extension), the format is:

"android.resource://[package]/[res type]/[res name]"

Uri path = Uri.parse("android.resource://com.androidbook.samplevideo/raw/myvideo");


This Uri can then be used to source the VideoView as follows:

VideoView myVid = (VideoView) findViewById(R.id.VideoView1);
myVid.setVideoURI(path);


You can now refer to any resource by URI.