How to find resource id of a file located in /res/raw folder by its filename?

自作多情 提交于 2019-11-28 12:48:07

问题


I want to get the resource id of the file located in /res/raw folder by filename. I have tried the following 2 methods but both of them return resource id as 0(zero).

Method 1:

String filename = "abc.txt";
int id = getResources().getIdentifier(filename, "raw", getPackageName());

Method 2:

String filename = "abc.txt";
String fullyQualifiedName = getPackageName() + ":raw/" + filename;
int id = getResources().getIdentifier(fullyQualifiedName, null, null);

If this is not the correct way, then how do we get the resource id by filename located in raw folder in Android.


回答1:


Drop the extension:

String filename = "abc";
int id = getResources().getIdentifier(filename, "raw", getPackageName());


来源:https://stackoverflow.com/questions/34862322/how-to-find-resource-id-of-a-file-located-in-res-raw-folder-by-its-filename

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!