Android: Howto get a resource id of an android res

2019-01-23 16:35发布

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

The use -

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

2) The statement for getting this file is this.getResources().openRawResource(R.raw.xyz) where xyz is the actual 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.

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.

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.

1条回答
Melony?
2楼-- · 2019-01-23 17:08

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

int id = this.getResources().getIdentifier("xyz", "raw", this.getPackageName());
查看更多
登录 后发表回答