Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Howto get a resource id of an android resource

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.

like image 828
user1576339 Avatar asked Aug 12 '12 09:08

user1576339


1 Answers

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

int id = this.getResources().getIdentifier("xyz", "raw", this.getPackageName());
like image 76
ekholm Avatar answered Oct 09 '22 20:10

ekholm