简体   繁体   English

我如何指定从R.res.drawable动态获取的资源?

[英]How can i specify the resource to get from R.res.drawable dynamically?

I have some videos stored dynamically on R.res.raw . 我有一些动态存储在R.res.raw上的R.res.raw

For example, I can acces to the video called v.mp4 with this line of code: 例如,我可以使用以下代码访问名为v.mp4的视频:

String fileName = "android.resource://" + this.getPackageName() + "/" + R.raw.v;

OK, but I will describe the video that i must open with a string ("v"). 好的,但是我将用字符串(“ v”)描述必须打开的视频。 Then, I tried to do this, but it didn't works: 然后,我尝试这样做,但是没有用:

String fileName = "android.resource://" + this.getPackageName() + "/" + R.raw+".v";

I can't do that, I get an error on R.raw 我不能这样做,我在R.raw上遇到错误

I also tried with this, but it doesn't works: 我也尝试过这样做,但是它不起作用:

String fileName = "android.resource://" + this.getPackageName() + "/res/raw/"+"v.mp4";

How can I solve this? 我该如何解决?

If you want to get any resource Path then there are two ways : 如果要获取任何资源路径,则有两种方法:

  1. Using Resource Name 使用资源名称

    Syntax : android.resource://[package]/[res type]/[res name] 语法: android.resource://[package]/[res type]/[res name]

    Example : If icon.png image file is available in res/drawable folder you can get path like : 示例:如果在res / drawable文件夹中有icon.png图像文件,则可以得到如下路径:

    String PATH="android.resource://com.my.package/drawable/icon";

  2. Using Resource Id 使用资源ID

    Syntax : android.resource://[package]/[resource_id] 语法: android.resource://[package]/[resource_id]

    Example : If icon.png image file is available in res/drawable folder you can get path: 示例:如果res / drawable文件夹中有icon.png图像文件,您可以获取路径:

    String PATH="android.resource://com.my.package/" + R.drawable.icon;

This were the examples to get the URI of any image file stored in drawable folder. 这是获取存储在drawable文件夹中的任何图像文件的URI的示例。

Similarly you can get URIs of res/raw folder also. 同样,您也可以获取res / raw文件夹的URI。

它必须给您一个错误,R.raw.v.mp4(必须为R.raw.v)提供了通用的int引用,但没有提供String,因此您无法通过提供String路径来获取它。并看到它生成了只有应用程序才能理解的int值。

您可以按以下方式使用它:

 android.resource://your package/raw/v

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM