如何使用一个变量的原始文件夹来访问视频?如何使用、变量、文件夹、原始

2023-09-08 09:35:54 作者:吻你到永远

这是我的code:

VideoView vd;
vd = (VideoView) findViewById(R.id.videoview2);
String path = "android.resource://" + getPackageName() + "/"
                        + R.raw.video1;
                vd.setVideoURI(Uri.parse(path));
                vd.start();

这工作,但我想R.raw.video1是一个字串becaus我有很多的视频播放。

This works, but i want the R.raw.video1 to be a string becaus i have a lot of videos to play.

所以我想是这样的:

String videoResource = "R.raw.video1"
 String path = "android.resource://" + getPackageName() + "/"
                            + videoResource;

不幸的是这不工作,我如何得到它的工作?

Unfortunately this doesn't work, how do i get it to work?

推荐答案

您需要的资源,要工作,可以通过它的名字用检索的标识符:

You need the identifier of the resource for that to work, which can be retrieved by its name using:

int id = getResources().
    getIdentifier("name_of_resource", "id", getPackageName());

所以,新的code将变成:

So your new code would become:

int videoResource = getResources().
    getIdentifier("video1", "raw", getPackageName());
String path = "android.resource://" + getPackageName() + "/" + videoResource;