安卓:HOWTO得到一个Android资源的资源ID [英] Android: Howto get a resource id of an android resource

查看:144
本文介绍了安卓:HOWTO得到一个Android资源的资源ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想,我已经保存在/ RES /原始文件夹中的Andr​​oid资源的资源ID。

I want to get a resource id of an android resource that I have saved in /res/raw folder.

使用 -

1),我传递资源的名称的类和这个名称被保存在一个字符串fileNeme

1) I am passing the name of the resource to the class and this name is being saved in a string fileNeme.

2)获取该文件中的说法是 this.getResources()。openRawResource(R.raw.xyz) xyz是实际文件名。

2) The statement for getting this file is this.getResources().openRawResource(R.raw.xyz) where xyz is the actual filename.

3)据我所知,openRawResource(int)方法接受一个int值,所以我不能用语句 this.getResources()。openRawResource(R.raw.filename)其中filename是上面定义一个字符串变量。

3) I understand that openRawResource(int) method takes an int value hence I cannot use the statement as this.getResources().openRawResource(R.raw.filename) where filename is a string variable defined above.

问题:我知道,这是保存在一个名为字符串文件名,文件名,但我不能打开它的方法获取资源ID以INT格式

Problem: I know the filename which is saved in a string called filename but I cannot open it as the method takes the resource id in int format.

我需要得到这个文件名的资源ID,并将其保存在一个int变量filename_int这个变量传递给openRawResource方法。

I need to get the resource id of this filename and save it in an int variable filename_int and pass this variable to openRawResource method.

请让我知道我能做到这一点。

Please let me know how can I achieve this.

推荐答案

您应该能够得到的资源ID与则getIdentifier()

You should be able to get the resource id with getIdentifier() :

int id = this.getResources().getIdentifier("xyz", "raw", this.getPackageName());

这篇关于安卓:HOWTO得到一个Android资源的资源ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆