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.

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:

  1. Uri introURI;  
  2. 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:

  1. String introURI;  
  2. 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:

  1. String introURI = "file:///sdcard/intro.3gp";  
  2. VideoView videoView;  
  3.   
  4. //...Omitted videoView initialization...  
  5.   
  6. videoView.setVideoPath(path);  

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

  1. Uri introURI,  
  2. //obtain the URI of the video file from the 'res' folder  
  3. introURI = Uri.parse("android.resource://your.app.package/" + R.raw.intro);  
  4. //or get it the URI from a the video file at the SD card  
  5. //introURI = Uri.parse("android.resource://your.app.package/" + R.raw.intro);  
  6.   
  7. VideoView videoView;  
  8.   
  9. //...Omitted videoView initialization...  
  10.   
  11. videoView.setURI(introURI);  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值