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.
You should be able to get the resource id with getIdentifier()
:
int id = this.getResources().getIdentifier("xyz", "raw", this.getPackageName());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With