如何通过文件名查找/res/raw文件夹中文件的资源ID? [英] How to find resource id of a file located in /res/raw folder by its filename?

查看:377
本文介绍了如何通过文件名查找/res/raw文件夹中文件的资源ID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过文件名获取位于/res/raw文件夹中的文件的资源ID.我尝试了以下2种方法,但它们都将资源ID返回为0(零).

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).

方法1:

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

方法2:

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

如果这不是正确的方法,那么如何通过Android中raw文件夹中的文件名获取资源ID.

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

推荐答案

删除扩展名:

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

这篇关于如何通过文件名查找/res/raw文件夹中文件的资源ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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