如何按名称从 res/raw 读取文件 [英] How to read file from res/raw by name
本文介绍了如何按名称从 res/raw 读取文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想打开文件夹 res/raw/ 中的文件.我绝对确定该文件存在.打开我试过的文件
I want to open a file from the folder res/raw/. I am absolutely sure that the file exists. To open the file I have tried
File ddd = new File("res/raw/example.png");
命令
ddd.exists();
产生假.所以这个方法行不通.
yields FALSE. So this method does not work.
尝试
MyContext.getAssets().open("example.png");
以 getMessage() "null" 结束异常.
简单地使用
R.raw.example
是不可能的,因为文件名仅在运行时作为字符串被知道.
is not possible because the filename is only known during runtime as a string.
为什么访问/res/raw/文件夹中的文件如此困难?
Why is it so difficult to access a file in the folder /res/raw/ ?
推荐答案
在给定链接的帮助下,我能够自己解决问题.正确的方法是用
With the help of the given links I was able to solve the problem myself. The correct way is to get the resource ID with
getResources().getIdentifier("FILENAME_WITHOUT_EXTENSION",
"raw", getPackageName());
将其作为 InputStream 获取
To get it as a InputStream
InputStream ins = getResources().openRawResource(
getResources().getIdentifier("FILENAME_WITHOUT_EXTENSION",
"raw", getPackageName()));
这篇关于如何按名称从 res/raw 读取文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文